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.
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: n
Would you like to enable monitoring using CloudWatch Application Insights? For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: n
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)
----------------------- Generating application: ----------------------- Name: sam-app Runtime: python3.9 Architectures: x86_64 Dependency Manager: pip Application Template: hello-world Output Directory: . Configuration file: sam-app/samconfig.toml Next steps can be found in the README file at sam-app/README.md
Commands you can use next ========================= [*] Create pipeline: cd sam-app && sam pipeline init --bootstrap [*] Validate SAM template: cd sam-app && sam validate [*] Test Function in the Cloud: cd sam-app && sam sync --stack-name {stack-name} --watch
sam-appのフォルダが作成されました。
1 2
$ ls sam-app
Hello World の内容
API Gateway, Lambda, IAM Roleで構成されるアプリケーションのテンプレートが作成されました。
$ cd ~/environment/sam-app $ sam local invoke --event events/event.json Invoking app.lambda_handler (python3.9) Local image was not found. Removing rapid images for repo public.ecr.aws/sam/emulation-python3.9 Building image............. Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.
Mounting /home/user/environment/sam-app/hello_world as /var/task:ro,delegated inside runtime container {"statusCode": 200, "body": "{\"message\": \"hello world\"}"}END RequestId: 6e1309d6-4f7f-4d7d-bac6-0c0e6b102a49 REPORT RequestId: 6e1309d6-4f7f-4d7d-bac6-0c0e6b102a49 Init Duration: 0.31 ms Duration: 181.50 ms Billed Duration: 182 ms Memory Size: 128 MB Max Memory Used: 128 MB
次に、API Gateway をシミュレートするローカル HTTP サーバーを実行します
1 2 3 4 5 6 7 8 9 10 11 12 13
$ cd ~/environment/sam-app $ sam local start-api --port 8080 Initializing the lambda functions containers. Local image is up-to-date Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.
Mounting /home/user/environment/sam-app/hello_world as /var/task:ro,delegated inside runtime container Containers Initialization is done. Mounting HelloWorldFunction at http://127.0.0.1:8080/hello [GET] You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template 2023-03-04 22:05:33 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:8080 2023-03-04 22:05:33 Press CTRL+C to quit
Looking for config file [samconfig.toml] : Found Reading default arguments : Success
Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: AWS Region [us-east-1]: ap-northeast-1 #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [Y/n]: #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: #Preserves the state of previously provisioned resources when an operation fails Disable rollback [y/N]: HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y Save arguments to configuration file [Y/n]: SAM configuration file [samconfig.toml]: SAM configuration environment [default]:
Looking for resources needed for deployment:
(中略)
Previewing CloudFormation changeset before deployment ====================================================== Deploy this changeset? [y/N]: y
デプロイが成功すると下記のような出力がされます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
CloudFormation outputs from deployed stack ----------------------------------------------------------------------------------------------------------------------------------------- Outputs ----------------------------------------------------------------------------------------------------------------------------------------- Key HelloWorldFunctionIamRole Description Implicit IAM Role created for Hello World function Value arn:aws:iam::123456789123:role/sam-app-HelloWorldFunctionRole-1QWE9DKNCP6V3 Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://tkgp1hj1zk.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
Key HelloWorldFunction Description Hello World Lambda Function ARN Value arn:aws:lambda:ap-northeast-1:123456789123:function:sam-app-HelloWorldFunction-5RxoDCiBWUPV -----------------------------------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - sam-app in ap-northeast-1
$ cd ~/environment/sam-app $ sam delete Are you sure you want to delete the stack sam-app in the region ap-northeast-1 ? [y/N]: y Are you sure you want to delete the folder sam-app in S3 which contains the artifacts? [y/N]: y