Skip to content

Commit 383bb35

Browse files
committed
WIP : SAM deployment
1 parent 9a54935 commit 383bb35

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

readme.md

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,14 @@ aws lambda create-function \
589589

590590
The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`.
591591

592+
To update the function, use the `update-function-code` command.
593+
594+
```sh
595+
aws lambda update-function-code \
596+
--function-name MyLambda \
597+
--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip
598+
```
599+
592600
#### Invoke the function
593601

594602
Use the `invoke-function` command to invoke the function. You can pass a well-formed JSON payload as input to the function. The payload must be encoded in base64. The CLI returns the status code and stores the response in a file.
@@ -624,14 +632,130 @@ aws iam delete-role --role-name lambda_basic_execution
624632

625633
### Deploy your Lambda function with AWS Serverless Application Model (SAM)
626634

627-
TODO
635+
AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications. It provides a simplified way to define the Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application. You can define your serverless application in a single file, and SAM will use it to deploy your function and all its dependencies.
628636

629-
#### Create the function
637+
To use SAM, you need to [install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) on your machine. The SAM CLI provides a set of commands to package, deploy, and manage your serverless applications.
630638

631-
#### Invoke the function
639+
Use SAM when you want to deploy more than a Lambda function. SAM helps you to create additional resources like an API Gateway, an S3 bucket, or a DynamoDB table, and manage the permissions between them.
640+
641+
#### Create the function
642+
643+
When using SAM, you describe the infrastructure you want to deploy in a YAML file. The file contains the definition of the Lambda function, the IAM role, and the permissions needed by the function. The SAM CLI uses this file to package and deploy your function.
644+
645+
You can create a SAM template to define a REST API implemented by AWS API Gateway and a Lambda function with the following command
646+
647+
```sh
648+
cat <<EOF > template.yaml
649+
AWSTemplateFormatVersion: '2010-09-09'
650+
Transform: AWS::Serverless-2016-10-31
651+
Description: SAM Template for APIGateway Lambda Example
652+
653+
Resources:
654+
# Lambda function
655+
APIGatewayLambda:
656+
Type: AWS::Serverless::Function
657+
Properties:
658+
# the directory name and ZIP file names depends on the Swift executable target name
659+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip
660+
Timeout: 60
661+
Handler: swift.bootstrap # ignored by the Swift runtime
662+
Runtime: provided.al2
663+
MemorySize: 512
664+
Architectures:
665+
- arm64
666+
# The events that will trigger this function
667+
Events:
668+
HttpApiEvent:
669+
Type: HttpApi # AWS API Gateway v2
670+
671+
Outputs:
672+
# display API Gateway endpoint
673+
APIGatewayEndpoint:
674+
Description: API Gateway endpoint UR"
675+
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
676+
EOF
677+
```
678+
679+
The Lambda function must accept an APIGateway v2 JSON payload as input parameter and return a valid APIGAteway v2 JSON response. See the example code in the [APIGateway example README file](Examples/APIGateway/README.md).
680+
681+
To deploy the function with SAM, use the `sam deploy` command. The very first time you deploy a function, you must use the `--guided` flag to configure the deployment. The command will ask you a series of questions to configure the deployment.
682+
683+
684+
Here is the command to deploy the function with SAM:
685+
686+
```sh
687+
# start the first deployment
688+
sam deploy --guided
689+
690+
Configuring SAM deploy
691+
======================
692+
693+
Looking for config file [samconfig.toml] : Not found
694+
695+
Setting default arguments for 'sam deploy'
696+
=========================================
697+
Stack Name [sam-app]: APIGAtewayLambda
698+
AWS Region [us-east-1]:
699+
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
700+
Confirm changes before deploy [y/N]: n
701+
#SAM needs permission to be able to create roles to connect to the resources in your template
702+
Allow SAM CLI IAM role creation [Y/n]: y
703+
#Preserves the state of previously provisioned resources when an operation fails
704+
Disable rollback [y/N]: n
705+
APIGatewayLambda has no authentication. Is this okay? [y/N]: y
706+
Save arguments to configuration file [Y/n]: y
707+
SAM configuration file [samconfig.toml]:
708+
SAM configuration environment [default]:
709+
710+
Looking for resources needed for deployment:
711+
712+
(redacted for brevity)
713+
714+
CloudFormation outputs from deployed stack
715+
--------------------------------------------------------------------------------
716+
Outputs
717+
--------------------------------------------------------------------------------
718+
Key APIGatewayEndpoint
719+
Description API Gateway endpoint UR"
720+
Value https://59i4uwbuj2.execute-api.us-east-1.amazonaws.com
721+
--------------------------------------------------------------------------------
722+
723+
724+
Successfully created/updated stack - APIGAtewayLambda in us-east-1
725+
```
726+
727+
To update your function or any other AWS service defined in your YAML file, you can use the `sam deploy` command without the `--guided` flag.
728+
729+
#### Invoke the function
730+
731+
SAM allows to invoke the function locally and remotely.
732+
733+
Local invocations allows to test your code before uploading it. It requires docker to run.
734+
735+
```sh
736+
sam local
737+
```
738+
739+
Remote invocations are done with the `sam invoke` command.
740+
741+
```sh
742+
sam invoke
743+
```
744+
745+
SAM allows you to access the function logs from Amazon Cloudwatch.
746+
747+
```sh
748+
sam logs
749+
```
632750
633751
#### Delete the function
634752
753+
SAM allows you to delete your function and all infrastructure that is defined in the YAML template with just one command.
754+
755+
```sh
756+
sam delete
757+
```
758+
635759
### Deploy your Lambda function with AWS Cloud Development Kit (CDK)
636760
637761
TODO

0 commit comments

Comments
 (0)