Serverless AWS Localstack Full Example - CDK, Dynamo, CloudFormation, Serverless
2 min read
8 months ago
Published on Apr 22, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Step-by-Step Tutorial: Serverless AWS Localstack Full Example
Prerequisites:
- Ensure you have Docker running on your local machine.
- Create a folder named
localstack
. - Create a
docker-compose.yaml
file with the necessary configurations. You can find the contents in the documentation of Localstack.
Setting Up Localstack:
- Run the Docker Compose app to instantiate Localstack.
docker-compose up
Creating an S3 Bucket:
- Create an S3 bucket using the Localstack CLI.
aws --endpoint-url=http://localhost:4566 s3 mb s3://static
- Configure the bucket permissions to allow public read access.
aws --endpoint-url=http://localhost:4566 s3api put-bucket-policy --bucket static --policy file://policy.json
- Enable the bucket for static website hosting.
aws --endpoint-url=http://localhost:4566 s3 website s3://static/ --index-document index.html --error-document error.html
Deploying a Static Website:
- Sync the website folder with the S3 bucket.
aws --endpoint-url=http://localhost:4566 s3 sync website s3://static
Creating a DynamoDB Table:
- Define the table structure in a JSON file, e.g.,
table-definition.json
. - Create the DynamoDB table using the Localstack CLI.
aws --endpoint-url=http://localhost:4566 dynamodb create-table --cli-input-json file://table-definition.json
- Verify the table creation by scanning it.
aws --endpoint-url=http://localhost:4566 dynamodb scan --table-name music
Inserting Items into DynamoDB:
- Prepare a JSON file with the items to insert, e.g.,
items.json
. - Insert the items into the DynamoDB table.
aws --endpoint-url=http://localhost:4566 dynamodb batch-write-item --request-items file://items.json
- Verify the insertion by scanning the table again.
Creating a Lambda Function:
- Define the IAM roles for the Lambda function in a JSON file.
- Create the IAM role for the Lambda function.
aws --endpoint-url=http://localhost:4566 iam create-role --role-name youtube-tutorial-role --assume-role-policy-document file://execution-role.json
Deploying the CDK Stack:
- Install the necessary CDK libraries.
npm install -g aws-cdk-local @aws-cdk/local
- Initialize a CDK project.
cdk local init sample-app --language typescript
- Define the CDK stack to wire up the Lambda function and API Gateway.
- Deploy the CDK stack to Localstack.
cdk local deploy
- Access the deployed resources through the API Gateway endpoint.
Testing the Setup:
- Make a request to the API Gateway endpoint using
curl
.curl http://localhost:4566/restapis/{api_id}/prod/user/request
Updating the Website:
- Update the JavaScript file to make a fetch request to the API Gateway endpoint.
- Deploy the updated website to the S3 bucket.
Congratulations! You have successfully set up a serverless architecture using AWS Localstack, CDK, DynamoDB, and Lambda functions.