Skip to content

Commit

Permalink
implement lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 22, 2023
1 parent 71773a6 commit eb29875
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 160 deletions.
73 changes: 64 additions & 9 deletions aws-lambda-project/.localstack/01_init.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
#!/usr/bin/env bash

awslocal s3 mb s3://testbucket
echo "List of S3 buckets:"
echo "-------------------------------"
awslocal s3 ls

awslocal sqs create-queue --queue-name test_queue
echo "List of SQS Queues:"
echo "-------------------------------"
awslocal sqs list-queues
# Navigate to the directory containing the JAR file
cd /localstackTemp || exit 1

# Set variables
JAR_FILE="aws-lambda-project-0.0.1-SNAPSHOT-aws.jar"
FUNCTION_NAME="localstack-lambda-url-example"
IAM_ROLE_ARN="arn:aws:iam::000000000000:role/lambda-role"

# Function to display an error message and exit
error_exit() {
echo "Error: $1" >&2
exit 1
}

# Check if the JAR file exists
if [ ! -f "$JAR_FILE" ]; then
error_exit "JAR file not found at $JAR_FILE"
fi

# Set environment variables (modify as needed)
ENV_VARS='{"Variables": {"SPRING_DATASOURCE_URL":"jdbc:postgresql://postgresqldb:5432/appdb", "SPRING_DATASOURCE_USERNAME":"appuser", "SPRING_DATASOURCE_PASSWORD":"secret"}}'

# Create Lambda function
awslocal lambda create-function \
--function-name "$FUNCTION_NAME" \
--runtime java17 \
--timeout 10 \
--handler org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest \
--role "$IAM_ROLE_ARN" \
--zip-file "fileb://$JAR_FILE" \
--environment "$ENV_VARS"

echo -e "Lambda function created successfully!\n"

# Create function URL configuration and capture the output
function_url_response=$(awslocal lambda create-function-url-config --function-name "$FUNCTION_NAME" --auth-type NONE)

# Use jq to extract the FunctionUrl
function_url=$(echo "$function_url_response" | grep -o '"FunctionUrl": "[^"]*' | cut -d'"' -f4)

# Print the extracted FunctionUrl
echo "FunctionUrl: $function_url"
echo -e "Function URL configuration created!\n"

# Wait until the function is active
awslocal lambda wait function-active-v2 --function-name "$FUNCTION_NAME"
echo -e "Lambda function is now active\n"

# Invoke Lambda function or use curl
#awslocal lambda invoke \
# --function-name "$FUNCTION_NAME" \
# --payload '{"body": "{\"name\": \"profile\"}" }' \
# output.txt
#
#echo -e "Lambda function invoked successfully\n"

# Use curl to invoke Lambda function and capture the response
response_from_curl=$(curl -X POST \
"$function_url" \
-H 'Content-Type: application/json' \
-d '{"name": "profile"}')

# Print the response from curl
echo "Response from curl: $response_from_curl"
2 changes: 2 additions & 0 deletions aws-lambda-project/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# aws-lambda-project

Request must be smaller than 69905067 bytes for the CreateFunction operation in aws lambda

### Format code

```shell
Expand Down
22 changes: 15 additions & 7 deletions aws-lambda-project/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ version: '3.8'
services:

postgresqldb:
image: postgres:16.0-alpine
image: postgres:16.1-alpine
environment:
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=appdb
ports:
- "5432:5432"
networks:
- aws-lambda

localstack:
image: localstack/localstack:2.3.2
image: localstack/localstack:3.0.0
ports:
- "4566:4566"
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
- SERVICES=s3,sqs
- DEFAULT_REGION=us-east-1
- DEBUG=${DEBUG-}
- DOCKER_HOST=unix:///var/run/docker.sock
- USE_SSL=0
- AWS_CBOR_DISABLE=1
- SERVICES=lambda
- LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=30
volumes:
- "../target:/localstackTemp"
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
- "../.localstack:/etc/localstack/init/ready.d" # ready hook
networks:
- aws-lambda

networks:
aws-lambda:
93 changes: 49 additions & 44 deletions aws-lambda-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<spring-cloud.version>2022.0.4</spring-cloud.version>
<spring-cloud-aws.version>3.0.3</spring-cloud-aws.version>
<springdoc-openapi.version>2.2.0</springdoc-openapi.version>
<commons-io.version>2.15.0</commons-io.version>
<wrapper.version>1.0.31.RELEASE</wrapper.version>
<aws.java.sdk.version>2.21.28</aws.java.sdk.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
<spotless.version>2.40.0</spotless.version>
Expand All @@ -46,38 +47,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter</artifactId>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -91,20 +73,6 @@
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -116,11 +84,6 @@
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -136,6 +99,17 @@
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand All @@ -154,14 +128,35 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
Expand All @@ -170,6 +165,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.learning.awslambda;

import com.learning.awslambda.config.ApplicationProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties({ApplicationProperties.class})
public class Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.learning.awslambda.config;

import com.learning.awslambda.model.request.ActorRequest;
import com.learning.awslambda.model.response.ActorResponse;
import com.learning.awslambda.services.ActorService;
import java.util.function.Function;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class AWSLambdaConfig {

@Bean
Function<ActorRequest, ActorResponse> findActorByName(ActorService actorService) {
return request -> actorService.findActorByName(request.name());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit eb29875

Please sign in to comment.