AWS Black Belt Techシリーズ AWS Lambda

Download AWS Black Belt Techシリーズ AWS Lambda

Post on 16-Jul-2015

1.919 views

Category:

Technology

6 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>AWS Lambda AWS Black Belt Tech Webinar 2015 ()2015.01.21</p></li><li><p> S3</p><p> DynamoDB</p></li><li><p> OS</p><p> 24365</p></li><li><p>AWS Lambda</p><p> Compute</p><p> EC2OS</p><p> ()</p><p> 201411Preview</p></li><li><p> AWS Lambda</p></li><li><p>Bring your own code</p><p> Node.js</p><p> / </p><p> /tmpread/write</p></li><li><p> 100</p></li><li><p> S3</p><p>AWS Lambda Amazon S3 Bucket </p><p> 1 </p><p>2 </p><p>3 </p></li><li><p> DynamoDB</p><p>AWS Lambda Amazon DynamoDB </p><p>Table and Stream </p></li><li><p> S3CloudTrail</p><p>AWS API </p><p>AWS CloudTrail Logs </p><p>AWS Lambda </p><p>Bucket </p></li><li><p> /DynamoDB Lambda</p><p>1. FB</p><p>Cognito</p><p>DynamoDBApp with AWS Mobile SDK</p><p>2. </p><p>4. DynamoDB</p><p>3. Lambda function</p><p>Lambda</p></li><li><p>Followers</p><p>4. DynamoDB- </p><p>1. FB</p><p>6. Push- </p><p>Cognito</p><p>Mobile Analytics</p><p>DynamoDB</p><p>S3</p><p>SNS7. Analytics</p><p>3. </p><p>2. S3</p><p>5. SNS</p><p>App with AWS Mobile </p><p>SDK</p></li><li><p>Lambda</p></li><li><p>Lambda</p><p> JavaScriptNode.js</p><p> Zip Zip</p><p> 128MB 64MB CPU</p><p> 360</p><p> LambdaIAM Role</p><p> /tmpread/write</p></li><li><p> Node.jsAmazon Linux</p><p> Amazon Linux</p><p> Amazon LinuxAMIEC2http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html</p></li><li><p>OpenCV</p><p>1. LambdaAMIEC2</p><p>2. OSNode.js</p><p>$ sudo yum update $ sudo yum install gcc44 gcc-c++ libgcc44 cmake python26-devel y $ wget http://nodejs.org/dist/v0.10.33/node-v0.10.33.tar.gz $ tar -zxvf node-v0.10.33.tar.gz $ cd node-v0.10.33 &amp;&amp; ./configure &amp;&amp; make $ sudo make install $ sudo easy_install pip $ pip install numpy </p></li><li><p>OpenCV</p><p>3. $ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip$ mkdir opencv_install$ mkdir opencv_example$ unzip opencv-2.4.9.zip d ./opencv_install/ &amp;&amp; cd opencv_install </p><p>4. $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_SHARED_LIBS=NO -D CMAKE_INSTALL_PREFIX=~/opencv opencv-2.4.9/ </p></li><li><p>OpenCV</p><p>5. </p><p>6. npmnpm installPKG_CONFIG_PATHOpenCVOpenCV</p><p>$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:~/opencv/lib/pkgconfig/$ npm install prefix=~/opencv_example opencv </p><p>$ make &amp;&amp; make install </p></li><li><p> Node.js</p></li><li><p> Timeout </p><p> Controlled termination 1context.done()</p><p> Default termination context.done()</p><p>Process exited before completing request</p><p> process.exit() </p><p>Process exited before completing request</p></li><li><p> Node.js</p><p> /tmp</p></li><li><p> Lambdafreeze </p></li><li><p>AWS Lambda</p></li><li><p> Node.js AWS SDK ImageMagick</p><p> S3DynamoDB</p><p> /tmp </p></li><li><p> handler_nameLambda </p><p> event JSON InvokeAsyncJSON</p><p> context.done</p><p>exports.handler_name = function(event, context) { console.log("value1 = " + event.key1); console.log("value2 = " + event.key2); ... context.done(null, "some message"); } </p></li><li><p>context.done()</p><p> context.done()</p><p> 1 null nullCloudWatch</p><p> 2 /</p></li><li><p>console.log('Loading event');var aws = require('aws-sdk');var s3 = new aws.S3({apiVersion: '2006-03-01'});exports.handler = function(event, context) { console.log('Received event:'); console.log(JSON.stringify(event, null, ' ')); var bucket = event.Records[0].s3.bucket.name; var key = event.Records[0].s3.object.key; s3.getObject({Bucket:bucket, Key:key}, function(err,data) { if (err) { console.log('error getting object ' + key + ' from bucket ' + bucket + '. Make sure they exist and your bucket is in the same region as this function.'); context.done('error','error getting file'+err); } else { console.log('CONTENT TYPE:',data.ContentType); context.done(null,''); } } );}; </p></li><li><p>Lambda</p><p> IAM role</p><p> Zip ZipSDKCLI Zip</p><p> zip -r lambda-sample.zip .</p><p> 128MB1GB64MB 1603</p></li><li><p> AWS 3</p><p> Amazon S3 Amazon Kinesis Amazon DynamoDB Stream(Preview)</p><p> Push/Pull2</p><p> JSON </p></li><li><p>Push</p><p> Amazon S3 InvokeAsync 3</p></li><li><p>Pull</p><p> Amazon DynamoDB Amazon Kinesis Lambda </p></li><li><p> S3 AWS LambdaS3</p><p> S3</p><p>Lambda</p><p> DynamoDB DynamoDB</p><p>StreamLambda</p><p> Kinesis AWS SDKCLI</p><p>Kinesis StreamLambda</p></li><li><p>Amazon DynamoDB Console (Preview) </p><p>Amazon S3 Console </p></li><li><p>CLI</p><p>$ aws lambda add-event-source \ --region us-east-1\ --function-name ProcessKinesisRecords \ --role invocation-role-arn \ --event-source kinesis-stream-arn \ --batch-size 100 \ </p></li><li><p>S3{ "Records": [ -- "s3": { "s3SchemaVersion": "1.0", "configurationId": "testConfigRule", "bucket": { "name": "sourcebucket", "ownerIdentity": { "principalId": "A3NL1KOZZKExample" }, "arn": "arn:aws:s3:::mybucket" }, "object": { "key": "sourcebucket/HappyFace.jpg", "size": 1024, "eTag": "d41d8cd98f00b204e9800998ecf8427e" } } } ] } </p></li><li><p>Kinesis{ "Records": [ { "awsRegion": "us-east-1", "sequenceNumber": "196800000000000000000374", "partitionKey": "2efdb0ea22685b46993e42a67302a001", "eventSource": "aws:kinesis", "data": "SOME CUSTOM DATA 1" }, { "awsRegion": "us-east-1", "sequenceNumber": "196800000000000000000571", "partitionKey": "2efdb0ea22685b46993e42a67302a003", "eventSource": "aws:kinesis", "data": "{ \"key\": \"value\" }" } ] } </p></li><li><p>IAM</p><p> Invocation RoleExecution Role2 Invocation Role</p><p> Execution Role</p><p> AWS</p><p> IAM IAM</p><p> PushPull</p><p> LambdaAWS Lambda</p></li><li><p>Invocation Role</p><p> Access PolicyTrust Policy</p><p> Push Lambda Trust Policy</p><p> Pull AWS LambdaPull Trust PolicyAWS Lambda</p></li><li><p>Invocation RolePushAccess Policy</p><p> Amazon S3lambda:InvokeAsync</p><p>{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": "*", "Action": [ "lambda:InvokeAsync" ] } ] } </p></li><li><p>Invocation RolePushTrust Policy</p><p> Amazon S3sts:AssumeRole Amazon S3</p><p>{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals":{ "sts:ExternalId": "arn:aws:s3:::bucket name" } } } ] } </p></li><li><p>Invocation RolePullAccess Policy</p><p> Amazon Kinesis</p><p>{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": "*", "Action": [ "kinesis:ReadStream"] } ] } </p></li><li><p>Invocation RolePullTrust Policy</p><p> AWS Lambdasts:AssumeRole</p><p>{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } </p></li><li><p>ExecutionRole</p><p> AWSIAM Access PolicyTrust Policy2</p><p> LambdaIAMAWS</p><p> Access Policy AWS Amazon CloudWatch Logs Amazon S3</p><p> Trust Policy AWS Lambda</p></li><li><p>Execution RoleAccess Policy</p><p> Amazon CloudWatch Logslogs:*</p><p>{ "Statement": [ { "Action": [ "logs:*" ], "Effect": "Allow", "Resource": "arn:aws:logs:*:*:*" } ] } </p></li><li><p>Execution RoleTrust Policy</p><p> AWS Lambda</p><p>{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } </p></li><li><p> Lambda</p><p> CloudWatchMetrics </p></li><li><p>exports.handler = function(event, context) { console.log('Received event:'); var bucket = event.Records[0].s3.bucket.name; var key = event.Records[0].s3.object.key; console.log(Bucket: +bucket); console.log(Key: +key); </p><p> Amazon CloudWatch Logs</p><p> Lambda</p><p> /</p><p> Max Memory Used Duration Billed Duration</p><p> console.log</p></li><li><p>Lambda</p><p> Execution rolesLambda</p><p> Invocation roles</p><p> S3DynamoDB</p></li><li><p> Lambda /tmp 512MB </p><p> 1024 </p><p> 1024 </p><p> 25/ </p><p>1 60 </p><p> zip 30MB </p><p>zip 250MB </p><p>InvokeAsyncJSON 128KB </p></li><li><p> () 100 $0.20/100(1$0.0000002)</p><p> () 100ms 100ms </p><p>Memory (MB) </p><p>Price per 100ms ($) </p><p>Free tier seconds per month </p><p>128 0.000000208" 3,200,000 </p><p>192 0.000000313" 2,133,333 </p><p>256 0.000000417" 1,600,000 </p><p>320 0.000000521" 1,280,000 </p><p>384 0.000000625" 1,066,667 </p><p>448 0.000000729" 914,286 </p><p>512 0.000000834" 800,000 </p><p>576 0.000000938" 711,111 </p><p>640 0.000001042" 640,000 </p><p>704 0.000001146" 581,818 </p><p>768 0.00000125" 533,333 </p><p>832 0.000001354" 492,308 </p><p>896 0.000001459" 457,143 </p><p>960 0.000001563" 426,667 </p><p>1024 0.000001667" 400,000 </p></li><li><p> https://aws.amazon.com/blogs/compute/</p><p> http://docs.aws.amazon.com/lambda/latest/dg/</p></li><li><p>Run Code in the cloud!</p></li></ul>