【Python】AWS SAMでLambda単体デプロイ【AWS】
この記事はAWS SAMによるLambdaの単体デプロイについて解説したものです。
こんにちは!らびです。今回はAWS SAMを使ってLambdaを単体デプロイしていきます。
前回取り上げたtemplate.yamlの知識を応用していきます!
Lambdaテンプレートの作成
まずはLambdaのテンプレートを作成しましょう。作業用のフォルダでsam initを実行します。
実行結果はこんな感じだジョ。
$sam init Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location Choice: 1 Cloning from https://github.com/aws/aws-sam-cli-app-templates Choose an AWS Quick Start application template 1 - Hello World Example 2 - Multi-step workflow 3 - Serverless API 4 - Scheduled task 5 - Standalone function 6 - Data processing 7 - Infrastructure event management 8 - Machine Learning Template: 1 Use the most popular runtime and package type? (Nodejs and zip) [y/N]: N Which runtime would you like to use? 1 - dotnet5.0 2 - dotnetcore3.1 3 - dotnetcore2.1 4 - go1.x 5 - java11 6 - java8.al2 7 - java8 8 - nodejs14.x 9 - nodejs12.x 10 - nodejs10.x 11 - python3.9 12 - python3.8 13 - python3.7 14 - python3.6 15 - python2.7 16 - ruby2.7 17 - ruby2.5 Runtime: 11 What package type would you like to use? 1 - Zip 2 - Image Package type: 1 Based on your selections, the only dependency manager available is pip. We will proceed copying the template using pip. Project name [sam-app]: sam-app-20220118-01 ----------------------- Generating application: ----------------------- Name: sam-app-20220118-01 Runtime: python3.9 Architectures: x86_64 Dependency Manager: pip Application Template: hello-world Output Directory: . Next steps can be found in the README file at ./sam-app-20220118-01/README.md Commands you can use next ========================= [*] Create pipeline: cd sam-app-20220118-01 && sam pipeline init --bootstrap [*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
template.yamlの修正
先ほど作成したLambdaのテンプレートでtemplate.yamlを修正して、API Gatewayに関する記述をコメントアウトします。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > sam-app-20220118-01 Sample SAM Template for sam-app-20220118-01 # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: Timeout: 3 Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.9 Architectures: - x86_64 # Events: # HelloWorld: # Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api # Properties: # Path: /hello # Method: get Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function # Find out more about other implicit resources you can reference within SAM # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api # HelloWorldApi: # Description: "API Gateway endpoint URL for Prod stage for Hello World function" # Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" HelloWorldFunction: Description: "Hello World Lambda Function ARN" Value: !GetAtt HelloWorldFunction.Arn HelloWorldFunctionIamRole: Description: "Implicit IAM Role created for Hello World function" Value: !GetAtt HelloWorldFunctionRole.Arn
ビルドとデプロイ
変更したtemplate.yamlの内容でビルドとデプロイを実施しましょう。
ビルドとデプロイのコマンドはそれぞれ以下の通りです。
$sam build $sam deploy --guided
ログや管理コンソールから単体デプロイが確認できるジョ。
ログ
… CloudFormation events from stack operations --------------------------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason --------------------------------------------------------------------------------------------------------------------- CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction - CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction Resource creation Initiated CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction - CREATE_COMPLETE AWS::CloudFormation::Stack sam-app-20220118-01 - --------------------------------------------------------------------------------------------------------------------- …
管理コンソール