Skip to content

Instantly share code, notes, and snippets.

Resources:
# ...
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Events:
FindApi:
Type: Api
Properties:
@jsonw23
jsonw23 / template.yaml
Created June 24, 2020 03:42
Define an explicit API in Resources
Resources:
Api:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref ApiStageName # use our parameter
@jsonw23
jsonw23 / template.yaml
Created June 24, 2020 03:39
Introduce a parameter for API Stage name
Parameters:
DeploymentStage:
Type: String
Default: staging
AllowedValues:
- staging
- production
  Description: Staging or Production deployment. Default is staging.
# New parameter for the API Gateway stage name
@jsonw23
jsonw23 / template.yaml
Last active June 24, 2020 03:22
Environment variable mapping based on DeploymentStage parameter
Mappings:
EnvironmentVariables:
# mapping keys are the allowed values for DeploymentStage
  staging:
# mapping value is multi-value where key is the environment variable
  PROD: false
  API_URL: {staging api endpoint url}
  production:
  PROD: true
  API_URL: {production api endpoint url}
@jsonw23
jsonw23 / template.yaml
Last active June 24, 2020 03:13
Add deployment stage as an environment variable
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Environment:
Variables:
# add a variable with the value of the DeploymentStage template parameter
@jsonw23
jsonw23 / template.yaml
Created June 24, 2020 03:05
Parameters defined for the template
Parameters:
DeploymentStage:
Type: String
Default: staging
AllowedValues:
- staging
  - production
Description: Staging or production deployment. Default is staging.
@jsonw23
jsonw23 / template.yaml
Created June 23, 2020 17:08
Add an IAM policy so the DynamoDB table can be scanned
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Environment:
Variables:
TABLE_NAME: !Ref MyTable
import os
import json
import boto3
def lambda_handler(event, context):
# environment variables are native, so use a language-specific
# method to access them, not the lambda context
table = os.environ['TABLE_NAME']
# use the AWS SDK to scan the table by name
@jsonw23
jsonw23 / template.yaml
Last active June 23, 2020 06:04
Add an environment variable to the function
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
# add an environment variable so the code can get the table name
Environment:
Variables:
@jsonw23
jsonw23 / template.yaml
Created June 23, 2020 05:27
Adding a resource to the template in the SAM sample hello-world project
Resources:
# the hello-world function from the sample project
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Events:
HelloWorld: