Clone this repo as a starting point for Lambda development.
A set of conventions for local AWS Lambda software development.
.
├── Makefile # Definition of `make` targets.
├── builds # Builds directory.
│ ├── deploy-2016-08-15_16-50.zip
│ └── deploy-2016-08-15_16-54.zip
├── cloudformation # CloudFormation template and parameters.
│ └── parameters.json
│ └── template.yaml
├── config.yaml # Static config and ParameterStore lookups.
├── index.py # Entry point for the Lambda function.
├── requirements # External dependencies.
│ ├── common.txt
│ ├── dev.txt
│ └── lambda.txt
└── tests # Unit tests for the package.
├── __init__.py
└── utils
├── __init__.py
├── test_localcontext.py
└── test_helpers.py
├── utils # Python package `utils`.
│ ├── __init__.py
│ ├── config.py
│ ├── localcontext.py
│ ├── helpers.pyCreates a CloudFormation stack with the Lambda function, an execution role, and an optional CloudWatch event to run on a recurring basis.
Edit cloudformation/parameters.json, and supply appropriate parameters.
In particular, Select appropriate values for ProjectName and EnvironmentName in cloudformation/parameters.json.
Important: The resulting CloudFormation stack will be named ${ProjectName}-${EnvironmentName}-stack, and a stack name of this form will be presumed for future CloudFormation operations.
Edit cloudformation/template.yaml and ensure that the Lambda function is appropriately permissioned via the policies attached to the LambdaFunctionExecutionRole.
If this Lambda function should have access to values in Parameter Store, set these on the CLI (or console); e.g.:
aws ssm put-parameter --name "/shared/preferred-salutation" \
--value "Hello" \
--type String
aws ssm put-parameter --name "/my-project/development/dynamo_table" \
--value "stack-ResultTable-16KAA4B56PNEP" \
--type StringEnsure that the AllowParameterAccess policy in cloudformation/template.yaml is uncommented and updated to reflect an appropriate parameter namespace(s); e.g.
# cloudformation/template.yaml
# ...
- PolicyName: "ParameterStore"
PolicyDocument:
Version: "2012-10-17"
Id: "AllowParameterAccess"
Statement:
- Sid: "AllowUnencryptedParameters"
Effect: "Allow"
Action: "ssm:GetParameter"
Resource:
- "Fn::Sub": "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/my-namespace.*"
- "Fn::Sub": "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${ProjectName}/shared/*"
# ...When these steps are complete:
make create-stackUpdate the stack as required:
make update-stackThe values in config.yaml will be available as a dictionary returned by the configuration() function (as shown in index.py).
Note that environment variables will be expanded, and any key prefixed with parameterstore_ will incur a ParameterStore GetParameter on that value. If encrypted, that value will be decrypted if kms:Decrypt is available to the executing role for the given key.
Sets up your local environment for local Python development by installing the development requirements from requirements/dev.txt.
Set up a new virtualenv (pyenv-virtualenv is great):
pyenv virtualenv my-project
pyenv activate my-projectAnd then install the local dependencies:
make initTo simulate permissioning parity between the Lambda environment and local, assume the Lambda role created by CloudFormation by creating a new entry in ~/.aws/config:
# ~/.aws/config
[profile regular]
output = json
region = us-west-2
[profile development]
output = json
region = us-west-2
source_profile = regular
role_arn = arn:aws:iam::111111111111:role/LambdaFunctionExecutionRo-34K8PIBFMONR
Set development as the current profile via export AWS_PROFILE=development.
Runs all the unit tests in the tests/ directory.
make testRuns the Python code on your local machine.
make invokeCreates a deployable Lambda zip file, and places into builds. Note that:
- Only
requirements/lambda.txtdependencies will be included .pycare removed from the repo beforebuildis initiated- All directories save for a blacklist (
.git/,tests/, etc) will be included, as will any.pyand.yamlfiles
make buildSends the build to a Lambda ARN. Note that $ARN must be set, or this will result in an error. It can be easily retrieved via make describe-stack.
ARN=arn:aws:lambda:us-west-2:111111111111:function:my-function-name make deployDeletes the CloudFormation stack.
make delete-stack