Tip
To deploy this project using GUI-based flow, navigate to console
- simple Lambda-based HTTP API.
- The application runs in a Lambda function and uses MongoDb Atlas cluster to store the data. To simplify the database access, this project uses Mongoose ORM.
- This project includes a pre-configured stacktape.yml configuration. The configured infrastructure is described in the stack description section
-
Fixed price resources:
- MongoDb Atlas cluster ($0.012/hour, ~$9/month)
-
There are also other resources that might incur costs (with pay-per-use pricing). If your load won't get high, these costs will be close to $0.
-
AWS account. If you don't have one, create new account here.
-
Stacktape account. If you don't have one, create new account here.
-
Stacktape installed.
Install on Windows (Powershell)
iwr https://installs.stacktape.com/windows.ps1 -useb | iex
Install on Linux
curl -L https://installs.stacktape.com/linux.sh | sh
Install on MacOS
curl -L https://installs.stacktape.com/macos.sh | sh
Install on MacOS ARM (Apple silicon)
curl -L https://installs.stacktape.com/macos-arm.sh | sh
- MongoDb Atlas account. To create one, refer to our step-by-step guide.
To initialize the project, use
stacktape init --starterId lambda-api-mongo-db
- Fill in your MongoDb Atlas credentials in the
providerConfig.mongoDbAtlas
section of the stacktape.yml config file. To learn how to get your API keys and organization ID, refer to MongoDB Atlas tutorial.
The deployment will take ~5-15 minutes. Subsequent deploys will be significantly faster.
Deploy from local machine
The deployment from local machine will build and deploy the application from your system. This means you also need to have:
- Docker. To install Docker on your system, you can follow this guide.- Node.js installed.
To perform the deployment, use the following command:
stacktape deploy --stage <<stage>> --region <<region>>
stage
is an arbitrary name of your environment (for example staging, production or dev-john)
region
is the AWS region, where your stack will be deployed to. All the available regions are listed below.
Region name & Location | code |
---|---|
Europe (Ireland) | eu-west-1 |
Europe (London) | eu-west-2 |
Europe (Frankfurt) | eu-central-1 |
Europe (Milan) | eu-south-1 |
Europe (Paris) | eu-west-3 |
Europe (Stockholm) | eu-north-1 |
US East (Ohio) | us-east-2 |
US East (N. Virginia) | us-east-1 |
US West (N. California) | us-west-1 |
US West (Oregon) | us-west-2 |
Canada (Central) | ca-central-1 |
Africa (Cape Town) | af-south-1 |
Asia Pacific (Hong Kong) | ap-east-1 |
Asia Pacific (Mumbai) | ap-south-1 |
Asia Pacific (Osaka-Local) | ap-northeast-3 |
Asia Pacific (Seoul) | ap-northeast-2 |
Asia Pacific (Singapore) | ap-southeast-1 |
Asia Pacific (Sydney) | ap-southeast-2 |
Asia Pacific (Tokyo) | ap-northeast-1 |
China (Beijing) | cn-north-1 |
China (Ningxia) | cn-northwest-1 |
Middle East (Bahrain) | me-south-1 |
South America (São Paulo) | sa-east-1 |
Deploy using AWS CodeBuild pipeline
Deployment using AWS CodeBuild will build and deploy your application inside AWS CodeBuild pipeline. To perform the deployment, use
stacktape codebuild:deploy --stage <<stage>> --region <<region>>
stage
is an arbitrary name of your environment (for example staging, production or dev-john)
region
is the AWS region, where your stack will be deployed to. All the available regions are listed below.
Region name & Location | code |
---|---|
Europe (Ireland) | eu-west-1 |
Europe (London) | eu-west-2 |
Europe (Frankfurt) | eu-central-1 |
Europe (Milan) | eu-south-1 |
Europe (Paris) | eu-west-3 |
Europe (Stockholm) | eu-north-1 |
US East (Ohio) | us-east-2 |
US East (N. Virginia) | us-east-1 |
US West (N. California) | us-west-1 |
US West (Oregon) | us-west-2 |
Canada (Central) | ca-central-1 |
Africa (Cape Town) | af-south-1 |
Asia Pacific (Hong Kong) | ap-east-1 |
Asia Pacific (Mumbai) | ap-south-1 |
Asia Pacific (Osaka-Local) | ap-northeast-3 |
Asia Pacific (Seoul) | ap-northeast-2 |
Asia Pacific (Singapore) | ap-southeast-1 |
Asia Pacific (Sydney) | ap-southeast-2 |
Asia Pacific (Tokyo) | ap-northeast-1 |
China (Beijing) | cn-north-1 |
China (Ningxia) | cn-northwest-1 |
Middle East (Bahrain) | me-south-1 |
South America (São Paulo) | sa-east-1 |
Deploy using Github actions CI/CD pipeline
- If you don't have one, create a new repository at https://github.com/new
- Create Github repository secrets: https://docs.stacktape.com/user-guides/ci-cd/#2-create-github-repository-secrets
- Replace
<<stage>>
and<<region>>
in the .github/workflows/deploy.yml file. git init --initial-branch=main
git add .
git commit -m "setup stacktape project"
git remote add origin git@github.com:<<namespace-name>>/<<repo-name>>.git
git push -u origin main
- To monitor the deployment progress, navigate to your github project and select the Actions tab
stage
is an arbitrary name of your environment (for example staging, production or dev-john)
region
is the AWS region, where your stack will be deployed to. All the available regions are listed below.
Region name & Location | code |
---|---|
Europe (Ireland) | eu-west-1 |
Europe (London) | eu-west-2 |
Europe (Frankfurt) | eu-central-1 |
Europe (Milan) | eu-south-1 |
Europe (Paris) | eu-west-3 |
Europe (Stockholm) | eu-north-1 |
US East (Ohio) | us-east-2 |
US East (N. Virginia) | us-east-1 |
US West (N. California) | us-west-1 |
US West (Oregon) | us-west-2 |
Canada (Central) | ca-central-1 |
Africa (Cape Town) | af-south-1 |
Asia Pacific (Hong Kong) | ap-east-1 |
Asia Pacific (Mumbai) | ap-south-1 |
Asia Pacific (Osaka-Local) | ap-northeast-3 |
Asia Pacific (Seoul) | ap-northeast-2 |
Asia Pacific (Singapore) | ap-southeast-1 |
Asia Pacific (Sydney) | ap-southeast-2 |
Asia Pacific (Tokyo) | ap-northeast-1 |
China (Beijing) | cn-north-1 |
China (Ningxia) | cn-northwest-1 |
Middle East (Bahrain) | me-south-1 |
South America (São Paulo) | sa-east-1 |
Deploy using Gitlab CI pipeline
- If you don't have one, create a new repository at https://gitlab.com/projects/new
- Create Gitlab repository secrets: https://docs.stacktape.com/user-guides/ci-cd/#2-create-gitlab-repository-secrets
- replace
<<stage>>
and<<region>>
in the .gitlab-ci.yml file. git init --initial-branch=main
git add .
git commit -m "setup stacktape project"
git remote add origin git@gitlab.com:<<namespace-name>>/<<repo-name>>.git
git push -u origin main
To monitor the deployment progress, navigate to your gitlab project and select CI/CD->jobs
stage
is an arbitrary name of your environment (for example staging, production or dev-john)
region
is the AWS region, where your stack will be deployed to. All the available regions are listed below.
Region name & Location | code |
---|---|
Europe (Ireland) | eu-west-1 |
Europe (London) | eu-west-2 |
Europe (Frankfurt) | eu-central-1 |
Europe (Milan) | eu-south-1 |
Europe (Paris) | eu-west-3 |
Europe (Stockholm) | eu-north-1 |
US East (Ohio) | us-east-2 |
US East (N. Virginia) | us-east-1 |
US West (N. California) | us-west-1 |
US West (Oregon) | us-west-2 |
Canada (Central) | ca-central-1 |
Africa (Cape Town) | af-south-1 |
Asia Pacific (Hong Kong) | ap-east-1 |
Asia Pacific (Mumbai) | ap-south-1 |
Asia Pacific (Osaka-Local) | ap-northeast-3 |
Asia Pacific (Seoul) | ap-northeast-2 |
Asia Pacific (Singapore) | ap-southeast-1 |
Asia Pacific (Sydney) | ap-southeast-2 |
Asia Pacific (Tokyo) | ap-northeast-1 |
China (Beijing) | cn-north-1 |
China (Ningxia) | cn-northwest-1 |
Middle East (Bahrain) | me-south-1 |
South America (São Paulo) | sa-east-1 |
After a successful deployment, some information about the stack will be printed to the terminal (URLs of the deployed services, links to logs, metrics, etc.).
To test the application, you will need the web service URL. It's printed to the terminal.
Make a POST
request to <<web_service_url>>/post
with the JSON data in its body to save the post. Use your preferred HTTP client or
the following cURL command:
curl -X POST <<web_service_url>>/posts -H 'content-type: application/json' -d '{ "title": "MyPost", "content": "Hello!", "authorEmail": "info@stacktape.com"}'
If the above cURL command did not work, try escaping the JSON content:
curl -X POST <<web_service_url>>/posts -H 'content-type: application/json' -d '{ \"title\":\"MyPost\",\"content\":\"Hello!\",\"authorEmail\":\"info@stacktape.com\"}'
Make a GET
request to <<web_service_url>>/posts
to get all posts.
curl <<web_service_url>>/posts
To run functions in the development mode (remotely on AWS), you can use the
dev command. For example, to develop and debug lambda function savePost
, you can use
stacktape dev --region <<your-region>> --stage <<stage>> --resourceName savePost
The command will:
- quickly re-build and re-deploy your new function code
- watch for the function logs and pretty-print them to the terminal
The function is rebuilt and redeployed, when you either:
- type
rs + enter
to the terminal - use the
--watch
option and one of your source code files changes
-
Stacktape deployments use AWS CloudFormation under the hood. It brings a lot of guarantees and convenience, but can be slow for certain use-cases.
-
To speed up the deployment, you can use the
--hotSwap
flag that avoids Cloudformation. -
Hotswap deployments work only for source code changes (for lambda function, containers and batch jobs) and for content uploads to buckets.
-
If the update deployment is not hot-swappable, Stacktape will automatically fall back to using a Cloudformation deployment.
stacktape deploy --hotSwap --stage <<stage>> --region <<region>>
- If you no longer want to use your stack, you can delete it.
- Stacktape will automatically delete every infrastructure resource and deployment artifact associated with your stack.
stacktape delete --stage <<stage>> --region <<region>>
Stacktape uses a simple stacktape.yml
configuration file to describe infrastructure resources, packaging, deployment
pipeline and other aspects of your project.
You can deploy your project to multiple environments (stages) - for
example production
, staging
or dev-john
. A stack is a running instance of an project. It consists of your application
code (if any) and the infrastructure resources required to run it.
The configuration for this project is described below.
- Every resource must have an arbitrary, alphanumeric name (A-z0-9).
- Stacktape resources consist of multiple underlying AWS or 3rd party resources.
API Gateway receives requests and routes them to the container.
For convenience, it has CORS allowed.
resources:
mainApiGateway:
type: http-api-gateway
properties:
cors:
enabled: true
The application data is stored in an Atlas MongoDB cluster.
Only the cluster tier needs to be configured in a minimal setup. You can also configure other properties if desired.
mongoDbCluster:
type: mongo-db-atlas-cluster
properties:
clusterTier: M2
The core of our application consists of two serverless functions:
- savePost function - saves post into database(MongoDB)
- getPosts function - get all posts from the database(MongoDB)
Functions are configured as follows:
-
Packaging - determines how the lambda artifact is built. The easiest and most optimized way to build the lambda from Typescript/Javascript is using
stacktape-lambda-buildpack
. We only need to configureentryfilePath
. Stacktape automatically transpiles and builds the application code with all of its dependencies, creates the lambda zip artifact, and uploads it to a pre-created S3 bucket on AWS. You can also use other types of packaging. -
ConnectTo list - we are adding the mongo cluster
mongoDbCluster
intoconnectTo
list. By doing this, Stacktape will automatically setup secure access to the cluster associated with the function's role as well as inject relevant environment variables into the function's runtime (such as connection string needed for connecting). -
Events - Events determine how is function triggered. In this case, we are triggering the function when an event (HTTP request) is delivered to the HTTP API gateway:
- if URL path is
/posts
and HTTP method isPOST
, request is delivered tosavePost
function. - if URL path is
/posts
and HTTP method isGET
, request is delivered togetPosts
function.
The event(request) including the request body is passed to the function handler as an argument.
- if URL path is
savePost:
type: function
properties:
packaging:
type: stacktape-lambda-buildpack
properties:
entryfilePath: ./src/lambdas/save-post.ts
memory: 512
connectTo:
- mongoDbCluster
events:
- type: http-api-gateway
properties:
httpApiGatewayName: mainApiGateway
path: /post
method: POST
getPosts:
type: function
properties:
packaging:
type: stacktape-lambda-buildpack
properties:
entryfilePath: ./src/lambdas/get-posts.ts
memory: 512
connectTo:
- mongoDbCluster
events:
- type: http-api-gateway
properties:
httpApiGatewayName: mainApiGateway
path: /posts
method: GET