This project contains the necessary files / steps for getting a serverless project up and running within AWS.
It contains:
- Lambda API project structure
- Deployment files
- Cloudformation templates for any other AWS resources (dynamodb, s3 etc..)
- CircleCI integration files.
- Install this project's dependencies with
npm install
. - Create an AWS account.
- Install the AWS-cli.
- Install the serverless package.
Follow the steps in this blog post to set up the AWS cli and credentials on your local machine. https://cloudacademy.com/blog/how-to-use-aws-cli/
The IAM role describes the resources which your project can access within your account.
For example, if you want your microservice to only have access to a certain s3 bucket, you would limit that in your project's IAM role.
## Cange this to your stack name
name: change-the-name-of-this-variable-to-your-service
## Change to your role ARN
role: THE-ARN-OF-YOUR-LAMBDA-IAM-ROLE
Example:
role: arn:aws:iam::{ACCT}:role/test-role
This is important, as it will tell your lambdas which role to reference.
Each lambda code is stored in an S3 bucket within your aws account.
This must be created manually first before deploying your lambda.
Once created, change the reference within the serverless.yml
file.
deploymentBucket:
name: YOUR_PROJECT-${self:custom.stage}-continuous-delivery
serverSideEncryption: AES256
NOTE: You will need a deployment bucket per environment.
This project uses CircleCI for continuous integration.
Once set up, add the following ENVIRONMENT VARIABLES within CircleCI.
AWS_ACCESS_KEY_ID
- Your aws access key id for your accountAWS_SECRET_ACCESS_KEY
- Your aws secret key for your account.AWS_DEFAULT_REGION
- Your default region. ex:us-east-1
This points to your cloudformation files on the local filesystem.
STACK_NAME=MY_PROJECT_STACK_NAME
ex: foo-serverless-resources-dev
ex: foo-serverless-resources-prod
By default, there is a DynamoDB definition within the cloudformation template. You need to change the name of the stack definition & stack name in that file, as well as the Dynamo table name so that it matches your projects needs.
npm run deploy:local
It will take a few minutes, but this process will bundle your project locally, and sync the files to AWS for you.
This will mimic your dev environment.
After your local deploy runs, the configuration for your lambda should be outputted.
Service Information
service: test-project
stage: dev
region: us-east-1
stack: test-project-dev
api keys:
test-project-dev-apikey: API_KEY
endpoints:
ANY - https://foo.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
functions:
api: test-project-dev-api
layers:
None
You can grab the endpoint and make a request to:
POST https://foo.execute-api.us-east-1.amazonaws.com/dev/users
Make sure you include the x-api-key
header with the API key above.
You can also check the Api-Gateway
console in AWS to see the lambdas that were created.
You can run npm start
to spin up your project locally.
You can then make requests to http://127.0.0.1:50512/users
with the x-api-key
header.
The default api key is outputted in your console.
I'm definitely not great at cloudops, and I know much of this cloudformation stuff could be done better. If you see areas for improvement, please make a PR. :)