Skip to content

About

Small sample application demonstrating LocalStack working with RDS, Lambda, API Gateway, VPC, private subnets, NAT Gateway, Internet Gateway.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

2 watching

Forks

Repository files navigation

Weather Validator Project

Overview

This project is a Weather Validator that uses AWS services (Lambda, RDS, S3, and API Gateway, VPC, NAT Gateway, Internet Gateway, etc). The architecture can be seen below:

Architecture

The application has a frontend component, using HTML, CSS, and JavaScript. The backend is composed of Lambda functions written in Java. The database is an RDS PostgreSQL instance.

Prerequisites

For Local Development (requires a LocalStack for AWS license):

For Deployment to AWS:

  • AWS CLI
  • Terraform (v1.9.3)

Deploying to AWS

Before proceeding, make sure you have the AWS CLI configured with the necessary permissions (use aws configure). Additionally, your account will need a user with AdministratorAccess policy attached. In other words:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "*",
            "Resource": "*"
        }
    ]
}

For a fast and easy deployment, you can use the following steps. The Lambda jar files are already included in the project.

cd terraform
terraform init
terraform plan
terraform apply --auto-approve

Once this is done, you will need to run the following command to create the database tables:

aws lambda invoke --function-name db-setup --region us-east-1 output.json 

See the CloudFront URL in the output of the Terraform run. You can access the application using this URL. That's it! You have successfully deployed the Weather Validator project to AWS.

If you need to make changes in the cloud

Backend:

  • change java files in the backend directory
  • run mvn clean install in the backend directory
  • run aws lambda update-function-code --function-name history-lambda --zip-file fileb://history-lambda/target/history-lambda-1.0.0.jar - change according to the function you are updating

Frontend:

  • change files in the frontend directory
  • run aws s3 sync ./ s3://weather-validator-bucket in the frontend directory

Infrastructure:

  • change .tf files in the terraform directory
  • run terraform apply --auto-approve in the terraform directory

Running the application locally

Backend - Lambda functions (make changes as you see fit):

    cd backend
    mvn clean install

Frontend

Install http-server:

npm install --global http-server

or

brew install http-server

Then:

cd frontend
http-server

You can change the files as needed. The frontend will be available at http://localhost:8080.

Run LocalStack and Deploy Infrastructure Locally

Start LocalStack for AWS with the LOCALSTACK_AUTH_TOKEN pre-configured:

export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
LOCALSTACK_DEBUG=1 lstk start

Deploy Infrastructure Locally:

Run the following commands:

    cd terraform-local
    lstk tf init
    lstk tf apply --auto-approve

Once this is done, you will need to run the following command to create the database tables:

lstk aws lambda invoke --function-name db-setup --region us-east-1 output.json 

or

 aws --endpoint=http://localhost.localstack.cloud:4566 lambda invoke --function-name db-setup --region us-east-1 output.json

Note! It could happen that the lstk tf apply executes successfully, but the resources are not ready yet, hence the endpoints will not be available to pass to the Lambdas and environment variables. Check the terraform.tfstate file, particularly for the HOST environment variable in the aws_lambda_function resources. It holds the RDS Proxy endpoint and port. In terraform-local the port is the cluster port, since LocalStack serves the database on the cluster port instead of 5432. In this case, please run the command again. You will see that a few resources have changed:

Apply complete! Resources: 0 added, 8 changed, 0 destroyed.

Test the application

With LocalStack running, build, deploy and test with:

make build
make deploy
make test

make test creates the tables with db-setup, checks the weather for a city through API Gateway, and reads the stored check back from the history API.

To run without an OpenWeather API key, point the weather Lambda at a stub before deploying. The test then serves the responses in tests/fixtures from S3 and checks both verdicts:

export TF_VAR_open_weather_base_url=http://localhost.localstack.cloud:4566/weather-stub/weather.json
make deploy
make test

Without the stub, make test calls the real OpenWeather API for London (override with CITY=<name>).

In the Check Weather tab, you can enter a city name and click the "Get Weather" button. The application will display the weather for the city you entered, along with the verdict: the weather must be over 20 degrees Celsius with no clouds. Otherwise, the application will display a message saying the weather is not nice.

Check Weather

The history tab will display the history of the cities you have checked the weather for. The full data from the Weather API can be copied to the clipboard by clicking the "Copy" button.

History

Cleaning Up

  • Destroy the cloud resources:
    cd terraform
    terraform destroy
  • Destroy local resources:
    cd terraform-local
    lstk tf destroy

Or just shut down the LocalStack container:

    lstk stop

About

Small sample application demonstrating LocalStack working with RDS, Lambda, API Gateway, VPC, private subnets, NAT Gateway, Internet Gateway.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages