An AWS Lambda function for trading on Coinbase Pro
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI.
The template.yaml
is a template that defines the application's AWS resources and configurations for those resources. Pay special attention to this file for configuring the project.
If you prefer to use an integrated development environment (IDE) to build and test the application, you can use the AWS Toolkit.
The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run the functions in an Amazon Linux environment that matches Lambda.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- Python 3 installed
- Docker - Install Docker community edition
This project utilizes a combination of in-template environment variable configuration settings, as well as referenced plaintext strings using AWS Systems Manager
To create secrets using the AWS CLI, follow the instructions here. You will need to populate secrets based on the referenced names in the template.yaml
and make sure to use the same region as specified in the samconfig.toml
.
The general syntax of the command to add a secret is:
aws ssm put-parameter \
--name "/example/hierarchy/secret" \
--type "String" \
--value "ami-12345abcdeEXAMPLE"
You can also list all parameters stored directly in a path with:
aws ssm get-parameters-by-path --path /example/hierarchy
These secret paths should match the paths found in the template.yaml
file as values for certain environment variables. An example is
CBP_API_KEY: '{{resolve:ssm:/coinbase_pro/api_keys/trade/key:1}}'
In this case, you would want to run the following command to store the secret with ssm
:
aws ssm put-parameter \
--name "/coinbase_pro/api_keys/trade/key" \
--type "String" \
--value "1234-abcd"
Note that this project already comes with a samconfig.toml
file for ease of use. With that file, you can build and deploy with the following:
sam build --use-container
sam deploy
The first sam
command will build the source of the application. The second sam
command will package and deploy the application to AWS.
To build and deploy with a walkthrough to customize changes to the config, run the following in your shell:
sam build --use-container
sam deploy --guided
The second sam
command will trigger a series of prompts to customize the deployment configuration:
- Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching the project name.
- AWS Region: The AWS region you want to deploy the app to.
- Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
- Allow SAM CLI IAM role creation: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the
CAPABILITY_IAM
value forcapabilities
must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass--capabilities CAPABILITY_IAM
to thesam deploy
command. - Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run
sam deploy
without parameters to deploy changes to the application.
Build the application with the sam build --use-container
command.
sam build --use-container
For rebuilding small changes, it should be a lot faster to use the following command:
sam build --use-container --parallel --skip-pull-image
The SAM CLI installs dependencies defined in requirements.txt
files for each function, creates a deployment package, and saves it in the .aws-sam/build
folder.
Before invoking a function, make sure you have any necessary secrets in an env.json
file as that will be necessary to override any environment variables referencing AWS Systems Manager strings. The SAM CLI does not currently support dynamic resolution of those references running locally. An example_env.json
is provided for you to show what it could look like for this project.
Test a single function by invoking it directly.
Run functions locally and invoke them with the sam local invoke
command. Note the use of --env-vars
to pull values from the file mentioned above.
sam local invoke --env-vars env.json ExampleFunction
Start an API locally.
sam local start-api
To simplify troubleshooting, SAM CLI has a command called sam logs
. sam logs
lets you fetch logs generated by the deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
NOTE
: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
sam logs -n ExampleFunction --stack-name myb-coinbase-pro-trade --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
Note that currently only local testing is supported.
Before running tests for the lambda functions, we need to host them locally. This can be done with the following command which starts by ensuring the code is built and then starts the lambda environment:
sam build --use-container --parallel --skip-pull-image
sam local start-lambda --env-vars env.json
Once that has been done, you can run the tests using:
pytest --verbose
Or a specific test using:
pytest --verbose tests/test_specific_function.py
To delete this application after it has been deployed successfully, use the AWS CLI. Assuming you used the project name for the stack name, you can run the following:
aws cloudformation delete-stack --stack-name myb-coinbase-pro-trade
See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.
Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: AWS Serverless Application Repository main page