Skip to content

Commit

Permalink
setup the application
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshCasper committed Apr 11, 2024
0 parents commit 399b979
Show file tree
Hide file tree
Showing 12 changed files with 707 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Run Integration Tests

on:
push:
paths-ignore:
- 'README.md'
branches:
- main
pull_request:
branches:
- main

jobs:
cdk:
name: Chaos Testing
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'

- name: Build Lambdas
run: cd lambda-functions && mvn clean package shade:shade

- name: Spin up LocalStack
run: |
docker-compose up -d
sleep 100
env:
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}

- name: Run Integration Tests
run: |
pip3 install boto3 pytest
pytest
env:
AWS_DEFAULT_REGION: us-east-1
AWS_REGION: us-east-1
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# LocalStack

volume/
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "3.9"

services:
localstack:
networks:
- ls_network
container_name: localstack
image: localstack/localstack-pro:latest
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
- "127.0.0.1:443:443"
environment:
- DOCKER_HOST=unix:///var/run/docker.sock #unix socket to communicate with the docker daemon
- LOCALSTACK_HOST=localstack # where services are available from other containers
- LAMBDA_DOCKER_NETWORK=ls_network
- LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN:?}
- LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=60
- PERSIST_ALL=false
- EXTENSION_AUTO_INSTALL=localstack-extension-outages
- LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=600
volumes:
- "./volume:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
- "./lambda-functions/target/product-lambda.jar:/etc/localstack/init/ready.d/target/product-lambda.jar"
- "./init-resources.sh:/etc/localstack/init/ready.d/init-resources.sh"

networks:
ls_network:
name: ls_network
132 changes: 132 additions & 0 deletions init-resources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/sh

apt-get -y install jq

echo "Create DynamoDB table..."
awslocal dynamodb create-table \
--table-name Products \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--region us-east-1

echo "Add Product Lambda..."
awslocal lambda create-function \
--function-name add-product \
--runtime java17 \
--handler lambda.AddProduct::handleRequest \
--memory-size 1024 \
--zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
--region us-east-1 \
--role arn:aws:iam::000000000000:role/productRole \
--environment Variables={AWS_REGION=us-east-1}

echo "Get Product Lambda..."
awslocal lambda create-function \
--function-name get-product \
--runtime java17 \
--handler lambda.GetProduct::handleRequest \
--memory-size 1024 \
--zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
--region us-east-1 \
--role arn:aws:iam::000000000000:role/productRole \
--environment Variables={AWS_REGION=us-east-1}

export REST_API_ID=12345

echo "Create Rest API..."
awslocal apigateway create-rest-api --name quote-api-gateway --tags '{"_custom_id_":"12345"}' --region us-east-1

echo "Export Parent ID..."
export PARENT_ID=$(awslocal apigateway get-resources --rest-api-id $REST_API_ID --region=us-east-1 | jq -r '.items[0].id')

echo "Export Resource ID..."
export RESOURCE_ID=$(awslocal apigateway create-resource --rest-api-id $REST_API_ID --parent-id $PARENT_ID --path-part "productApi" --region=us-east-1 | jq -r '.id')

echo "RESOURCE ID:"
echo $RESOURCE

echo "Put GET Method..."
awslocal apigateway put-method \
--rest-api-id $REST_API_ID \
--resource-id $RESOURCE_ID \
--http-method GET \
--request-parameters "method.request.path.productApi=true" \
--authorization-type "NONE" \
--region=us-east-1

echo "Put POST Method..."
awslocal apigateway put-method \
--rest-api-id $REST_API_ID \
--resource-id $RESOURCE_ID \
--http-method POST \
--request-parameters "method.request.path.productApi=true" \
--authorization-type "NONE" \
--region=us-east-1


echo "Update GET Method..."
awslocal apigateway update-method \
--rest-api-id $REST_API_ID \
--resource-id $RESOURCE_ID \
--http-method GET \
--patch-operations "op=replace,path=/requestParameters/method.request.querystring.param,value=true" \
--region=us-east-1


echo "Put POST Method Integration..."
awslocal apigateway put-integration \
--rest-api-id $REST_API_ID \
--resource-id $RESOURCE_ID \
--http-method POST \
--type AWS_PROXY \
--integration-http-method POST \
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:add-product/invocations \
--passthrough-behavior WHEN_NO_MATCH \
--region=us-east-1

echo "Put GET Method Integration..."
awslocal apigateway put-integration \
--rest-api-id $REST_API_ID \
--resource-id $RESOURCE_ID \
--http-method GET \
--type AWS_PROXY \
--integration-http-method GET \
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:get-product/invocations \
--passthrough-behavior WHEN_NO_MATCH \
--region=us-east-1

echo "Create DEV Deployment..."
awslocal apigateway create-deployment \
--rest-api-id $REST_API_ID \
--stage-name dev \
--region=us-east-1

awslocal sns create-topic --name ProductEventsTopic

awslocal sqs create-queue --queue-name ProductEventsQueue

awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn

awslocal sns subscribe \
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
--protocol sqs \
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal lambda create-function \
--function-name process-product-events \
--runtime java17 \
--handler lambda.DynamoDBWriterLambda::handleRequest \
--memory-size 1024 \
--zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
--region us-east-1 \
--role arn:aws:iam::000000000000:role/productRole

awslocal lambda create-event-source-mapping \
--function-name process-product-events \
--batch-size 10 \
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal sqs set-queue-attributes \
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
--attributes VisibilityTimeout=10
128 changes: 128 additions & 0 deletions lambda-functions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<artifactId>product-lambda</artifactId>
<groupId>cloud.localstack</groupId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
</properties>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>protocol-core</artifactId>
<version>2.20.69</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.20.68</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.3</version>
</dependency>
<!-- Jackson Core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
</dependency>

<!-- Jackson Databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>

<!-- Jackson Annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
<version>2.20.69</version>
</dependency>
<!-- SLF4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<!-- Logback Classic Implementation -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.7</version>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.20.47</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>product-lambda</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 399b979

Please sign in to comment.