Skip to content

Instantly share code, notes, and snippets.

@jsonw23
jsonw23 / make_rel_path.py
Created February 13, 2015 15:32
Get the relative path from one full path to another full path using Python Path object
def make_rel_path(from_path, to_path):
from_path = Path(from_path)
to_path = Path(to_path)
rel_url = to_path.name
for parent2 in to_path.parents:
up = 0
for parent1 in from_path.parents:
up += 1
if parent1 == parent2:
common = parent1
@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:
@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:
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
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
@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
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
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
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
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