|
| 1 | +# How to create serverless applications using AWS SAM |
| 2 | +AWS Serverless Application Model (AWS SAM) allows you to easily create and |
| 3 | +manage resources used in your serverless application using AWS CloudFormation. |
| 4 | +You can define your serverless application as a SAM template - a JSON or YAML |
| 5 | +configuration file that describes Lambda function, API endpoints and |
| 6 | +other resources in your application. Using nifty commands, you upload this |
| 7 | +template to CloudFormation which creates all the individual resources and |
| 8 | +groups them into a *CloudFormation Stack* for ease of management. |
| 9 | +When you update your SAM template, you will re-deploy the changes to |
| 10 | +this stack. AWS CloudFormation will take care of updating the individual |
| 11 | +resources for you. |
| 12 | + |
| 13 | + |
| 14 | +The remainder of document explains how to write SAM templates and |
| 15 | +deploy them via AWS CloudFormation. |
| 16 | + |
| 17 | +## Writing SAM Template |
| 18 | +Checkout the [latest specification](versions/2016-10-31.md) for details on how to write a SAM template |
| 19 | + |
| 20 | +## Packing Artifacts |
| 21 | +Before you can deploy a SAM template, you should first upload your Lambda |
| 22 | +function code zip and API's OpenAPI File to S3. Set the `CodeUri` and |
| 23 | +`DefinitionUri` properties to the S3 URI of uploaded files. You |
| 24 | +can choose to do this manually or use `aws cloudformation package` [CLI command](http://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) to automate the task of uploading local artifacts to S3 bucket. The command returns a copy of your template, replacing references to local artifacts with S3 location where the command uploaded your artifacts. |
| 25 | + |
| 26 | +To use this command, set `CodeUri` property to be the path to your |
| 27 | +source code folder/zip/jar and `DefinitionUri` property to be a path to |
| 28 | +your OpenAPI file, as shown in the example below |
| 29 | + |
| 30 | +``` |
| 31 | +MyLambdaFunction: |
| 32 | + Type: AWS::Serverless::Function |
| 33 | + Properties: |
| 34 | + CodeUri: ./code |
| 35 | + ... |
| 36 | +
|
| 37 | +MyApi: |
| 38 | + Type: AWS::Serverless::Api |
| 39 | + Properties: |
| 40 | + DefinitionUri: ./specs/swagger.yaml |
| 41 | + ... |
| 42 | +``` |
| 43 | + |
| 44 | +Run the following command to upload your artifacts to S3 and output a |
| 45 | +packaged template that can be readily deployed to CloudFormation. |
| 46 | +``` |
| 47 | +aws cloudformation package \ |
| 48 | + --template-file /path_to_template/template.yaml \ |
| 49 | + --s3-bucket bucket-name \ |
| 50 | + --output-template-file packaged-template.yaml |
| 51 | +``` |
| 52 | + |
| 53 | +The packaged template will look something like this: |
| 54 | +``` |
| 55 | +MyLambdaFunction: |
| 56 | + Type: AWS::Serverless::Function |
| 57 | + Properties: |
| 58 | + CodeUri: s3://<mybucket>/<my-zipfile-path> |
| 59 | + ... |
| 60 | +
|
| 61 | +MyApi: |
| 62 | + Type: AWS::Serverless::Api |
| 63 | + Properties: |
| 64 | + DefinitionUri: s3://<mybucket>/<my-openapi-file-path> |
| 65 | + ... |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +## Deploying to AWS CloudFormation |
| 70 | +SAM template is deployed to AWS CloudFormation by [creating a changeset](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets-create.html) |
| 71 | +using the SAM template followed by [executing the changeset](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets-execute.html). |
| 72 | +Think of a ChangeSet as a diff between your current stack template and the new template that you are deploying. After you create a ChangeSet, you have the opportunity to examine the diff before executing it. Both the AWS Console and AWS CLI provide commands to create and execute a changeset. |
| 73 | + |
| 74 | +Alternatively, you can use `aws cloudformation deploy` CLI command to deploy the SAM template. Under-the-hood it creates and executes a changeset and waits until the deployment completes. It also prints debugging hints when the deployment fails. Run the following command to deploy the packaged template to a stack called `my-new-stack`: |
| 75 | + |
| 76 | +``` |
| 77 | +aws cloudformation deploy \ |
| 78 | + --template-file /path_to_template/packaged-template.yaml \ |
| 79 | + --stack-name my-new-stack \ |
| 80 | + --capabilities CAPABILITY_IAM |
| 81 | +``` |
| 82 | + |
| 83 | +Refer to the [documentation](http://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html) for more details. |
0 commit comments