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:

  1. Ensure you have Docker running on your local machine.
  2. Create a folder named localstack.
  3. Create a docker-compose.yaml file with the necessary configurations. You can find the contents in the documentation of Localstack.

Setting Up Localstack:

  1. Run the Docker Compose app to instantiate Localstack.
    docker-compose up
    

Creating an S3 Bucket:

  1. Create an S3 bucket using the Localstack CLI.
    aws --endpoint-url=http://localhost:4566 s3 mb s3://static
    
  2. 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
    
  3. 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:

  1. Sync the website folder with the S3 bucket.
    aws --endpoint-url=http://localhost:4566 s3 sync website s3://static
    

Creating a DynamoDB Table:

  1. Define the table structure in a JSON file, e.g., table-definition.json.
  2. Create the DynamoDB table using the Localstack CLI.
    aws --endpoint-url=http://localhost:4566 dynamodb create-table --cli-input-json file://table-definition.json
    
  3. Verify the table creation by scanning it.
    aws --endpoint-url=http://localhost:4566 dynamodb scan --table-name music
    

Inserting Items into DynamoDB:

  1. Prepare a JSON file with the items to insert, e.g., items.json.
  2. Insert the items into the DynamoDB table.
    aws --endpoint-url=http://localhost:4566 dynamodb batch-write-item --request-items file://items.json
    
  3. Verify the insertion by scanning the table again.

Creating a Lambda Function:

  1. Define the IAM roles for the Lambda function in a JSON file.
  2. 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:

  1. Install the necessary CDK libraries.
    npm install -g aws-cdk-local @aws-cdk/local
    
  2. Initialize a CDK project.
    cdk local init sample-app --language typescript
    
  3. Define the CDK stack to wire up the Lambda function and API Gateway.
  4. Deploy the CDK stack to Localstack.
    cdk local deploy
    
  5. Access the deployed resources through the API Gateway endpoint.

Testing the Setup:

  1. Make a request to the API Gateway endpoint using curl.
    curl http://localhost:4566/restapis/{api_id}/prod/user/request
    

Updating the Website:

  1. Update the JavaScript file to make a fetch request to the API Gateway endpoint.
  2. 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.