Install AWS SAM CLI
Using brew
brew tap aws/tap
brew install aws-sam-cli
Deploy APP To AWS Lambda
Step 1 Init Sam APP And Download Template
Can Use sam init
Or sam init -r <runtime> -n <app-name>
sam init
#or
sam init -r <runtime> -n <function name>
In This Case, I Want Create Python 3.8 Lambda, So I Just Enter sam init -r python3.8 -n new-function
After Enter sam init
, You Need To Choice Look Like These:
~/ » sam init -r python3.8 -n demo
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
What package type would you like to use?
1 - Zip (artifact is a zip uploaded to S3)
2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 1
Cloning from https://github.com/aws/aws-sam-cli-app-templates
AWS quick start application templates:
1 - Hello World Example
2 - EventBridge Hello World
3 - EventBridge App from scratch (100+ Event Schemas)
4 - Step Functions Sample App (Stock Trader)
5 - Elastic File System Sample App
Template selection: 1
-----------------------
Generating application:
-----------------------
Name: demo
Runtime: python3.8
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
Next steps can be found in the README file at ./README.md
Now You Can See Your Folder Like These
~/» ls
README.md __init__.py events hello_world template.yaml tests
In This Case, Just Need hello_world
And template.yaml
-
hello_world
Folder
-
put your Code, reqirment.txt etc.
-
template.yam
After Write Code, We Need Build Image.
Build Image Using sam build --use-container --build-image Function1=amazon/aws-sam-cli-build-image-python3.8
~/ » sam build --use-container --build-image Function1=amazon/aws-sam-cli-build-image-python3.8
Starting Build inside a container
Building codeuri: ~/ runtime: python3.8 metadata: {} functions: ['ITHomeFunction']
Fetching public.ecr.aws/sam/build-python3.8:latest Docker container image......
Mounting ~/ as /tmp/samcli/source:ro,delegated inside runtime container
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Deploy Using sam deploy --guided
~/ » sam deploy --guided
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Found
Reading default arguments : Success
Setting default arguments for 'sam deploy'
=========================================
Stack Name [ITHome]:
AWS Region [us-east-1]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [Y/n]: y
#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]: y
Save arguments to configuration file [Y/n]: y
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:
Looking for resources needed for deployment: Found!
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1n4q2wolub9lw
A different default S3 bucket can be set in samconfig.toml
Saved arguments to config file
Running 'sam deploy' for future deployments will use the parameters saved above.
The above parameters can be changed by modifying samconfig.toml
Learn more about samconfig.toml syntax at
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
Initiating deployment
=====================
Uploading to Demo/befabc84a86957c750b68443c0db70c2.template 633 / 633 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------
* Modify DemoFunctionSchedule AWS::Lambda::Permissio Conditional
1Permission n
* Modify DemoFunctionSchedule AWS::Events::Rule False
1
* Modify DemoFunction AWS::Lambda::Function False
-------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:us-east-1:120340392319:changeSet/samcli-deploy1622992910/276259dd-4bcf-43d3-9e77-ab038c68bac6
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2021-06-06 23:22:16 - Waiting for stack create/update to complete
CloudFormation events from changeset
-------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------
UPDATE_IN_PROGRESS AWS::Lambda::Function ITHomeFunction -
UPDATE_COMPLETE AWS::Lambda::Function ITHomeFunction -
UPDATE_COMPLETE AWS::CloudFormation::S ITHome -
tack
UPDATE_COMPLETE_CLEANU AWS::CloudFormation::S ITHome -
P_IN_PROGRESS tack
-------------------------------------------------------------------------------------------------
Successfully created/updated stack - ITHome in us-east-1