1. AWS側の準備
Lambda用に、IAMロールを用意しておきます。
このロールARN を、template.yaml で指定します。
図) IAMロール1/2 |
図) IAMロール2/2 |
今回使用するLambda関数は、VPC内に置くので、ロールには、EC2 FullAccess の権限をつけています。
なお、サブネット、セキュリティグループも用意しておき、template.yaml で指定します。
なお、サブネット、セキュリティグループも用意しておき、template.yaml で指定します。
2. AWS SAM ローカルでデプロイ
Lambda 用にパッケージ化して S3 に格納します。
[root@centos702 lambda]# sam package --template-file template.yaml --s3-bucket blue21.dev.local --output-template-file /tmp/output-template.yaml A newer version of the AWS SAM CLI is available! Your version: 0.2.4 Latest version: 0.2.6 See https://github.com/awslabs/aws-sam-local for upgrade instructions Uploading to f3344cfff66969a46a211ced788523c9 5730692 / 5730692.0 (100.00%) Successfully packaged artifacts and wrote output template to file /tmp/output-template.yaml. Execute the following command to deploy the packaged template aws cloudformation deploy --template-file /tmp/output-template.yaml --stack-name <YOUR STACK NAME> [root@centos702 lambda]# aws s3 ls s3://blue21.dev.local 2018-01-28 08:57:44 39 HelloWorld.sh 2018-01-28 10:25:10 5730692 f3344cfff66969a46a211ced788523c9
output-template.yaml を見ると、CodeUri が更新されてます。
[root@centos702 lambda]# cat /tmp/output-template.yaml AWSTemplateFormatVersion: '2010-09-09' Description: ssh application. Resources: WorkerFunction: Properties: CodeUri: s3://blue21.dev.local/f3344cfff66969a46a211ced788523c9 FunctionName: SSHFunction Handler: worker_function.worker_handler MemorySize: 128 Role: arn:aws:iam::xxxxxxxx:role/Lambda01_Role Runtime: python2.7 Timeout: 180 VpcConfig: SecurityGroupIds: - sg-bbf176df SubnetIds: - subnet-f528a2ad - subnet-ceb59087 Type: AWS::Serverless::Function Transform: AWS::Serverless-2016-10-31
output-template.yaml を指定して、cloudformation で Lambda に登録します。
[root@centos702 lambda]# aws cloudformation deploy --template-file /tmp/output-template.yaml --stack-name LambdaSSHFunction Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - LambdaSSHFunction
3. 確認
AWSコンソールでデプロイした内容を確認してみます。
CloudFormation を見ると、下図のように LambdaSSHFunction スタックが登録されています。
図) CloudFormation |
template.yaml で指定した 関数名です。
図) Lambda 1/5 |
図) Lambda 2/5 |
図) Lambda 3/5 |
図) Lambda 4/5 |
template.yaml で指定した サブネットとセキュリティグループは下図のように確認できます。
図) Lambda 5/5 |