Skip to content

Commit

Permalink
Improved code by adding more configurable environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
enriquh committed Jan 12, 2021
1 parent 6b4d361 commit 8cea3db
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/target/
/.aws-sam/
/.idea/
/*.tar.gz
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ This project contains an AWS Lambda maven application with [AWS Java SDK 2.x](ht
The generated function handler class just returns the input. The configured AWS Java SDK client is created in `DependencyFactory` class and you can
add the code to interact with the SDK client based on your use case.

In order to generate this project the following steps were performed (notice that you need to comply with the pre-requisites before performing these steps)

1. Generate project using mvn archetype:

```
mvn -B archetype:generate -DarchetypeGroupId=software.amazon.awssdk -DarchetypeArtifactId=archetype-lambda -Dservice=redshiftdata -Dregion=US_EAST_1 -DgroupId=com.example.myapp -DartifactId=myapp
```

Note the -Dservice=redshiftdata and -Dregion=US_EAST_1 options, replace it by the appropriate region as needed, supported services for archetype can be [found here](https://mvnrepository.com/artifact/software.amazon.awssdk/bom/2.15.61)

2. Add AmazonRedshiftDataFullAccess policy to the template.yaml generated by archetype to allow you to use the RS data API, [more info here](https://docs.aws.amazon.com/redshift/latest/mgmt/data-api.html#data-api-access)

3. (optional) Add the maven-jar-plugin to the project POM if you want to be able to execute the JAR locally (to specify Mainclass), [more info here](http://www.avajava.com/tutorials/lessons/how-do-i-specify-a-main-class-in-the-manifest-of-my-generated-jar-file.html)

#### Building the project
```
mvn clean install
Expand All @@ -39,6 +53,11 @@ To deploy the application, you can run the following command:
sam deploy --guided
```

You will need to provide the following parameters:

* ARN of the [AWS Secret Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) secret containing the username and password for your redshift cluster id


See [Deploying Serverless Applications](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-deploying.html) for more info.


Expand Down
Binary file removed java_test.tar.gz
Binary file not shown.
10 changes: 6 additions & 4 deletions src/main/java/com/example/myapp/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
import software.amazon.awssdk.services.redshiftdata.model.*;
Expand Down Expand Up @@ -35,10 +34,13 @@ public Object handleRequest(final Object input, final Context context) {
client = App.getRedshiftDataClient();

String secretArn = System.getenv("RS_SECRET_ARN");
String clusterId = System.getenv("RS_CLUSTER_ID");
String databaseName = System.getenv("RS_CLUSTER_DATABASE");
String sql = System.getenv("SQL_STATEMENT");

ExecuteStatementRequest executeStatementRequest= ExecuteStatementRequest.builder().clusterIdentifier("redshift-cluster-1").database("dev")
ExecuteStatementRequest executeStatementRequest= ExecuteStatementRequest.builder().clusterIdentifier(clusterId).database(databaseName)
.secretArn(secretArn)
.sql("select * from nyc_yellow_taxi limit 10;").build();
.sql(sql).build();

ExecuteStatementResponse response = client.executeStatement(executeStatementRequest);

Expand Down Expand Up @@ -81,7 +83,7 @@ public Object handleRequest(final Object input, final Context context) {

public static RedshiftDataClient getRedshiftDataClient() {
if (redshiftDataClient == null)
redshiftDataClient = RedshiftDataClient.builder().region(Region.US_EAST_1).credentialsProvider(DefaultCredentialsProvider.builder().profileName("MyAccount").build()).build();
redshiftDataClient = RedshiftDataClient.builder().region(Region.of(System.getenv("RS_CLUSTER_REGION"))).credentialsProvider(DefaultCredentialsProvider.builder().profileName("MyAccount").build()).build();

return redshiftDataClient;
}
Expand Down
27 changes: 27 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,40 @@ Metadata:
default: "Redshift Data API Lambda example"
Parameters:
- RedshiftSecretArn
- RedshiftClusterId
- RedshiftClusterDatabase
- SQLStatement

ParameterLabels:
RedshiftSecretArn:
default: "Secret Mager ARN for RS cluster"
RedshiftClusterId:
default: "Id for the Redshift cluster to be used"
RedshiftClusterDatabase:
default: "Database name to be used in the provided redshift cluster"
SQLStatement:
default: "SQL statement to be executed by the RS data API"

Parameters:
RedshiftSecretArn:
Type: String
Description: 'Enter the ARN of the secret containing the user and password fot the RS cluster'
MinLength: 20
MaxLength: 2048
RedshiftClusterId:
Type: String
Description: 'Enter the id of your Redshift cluster'
MinLength: 1
MaxLength: 63
AllowedPattern: '([a-z]|[A-Z]|[0-9]|-)*'
RedshiftClusterDatabase:
Type: String
Description: 'Enter the database name to be used in your Redshift cluster'
MinLength: 1
MaxLength: 127
SQLStatement:
Type: String
Description: 'Enter the SQL statement to be executed'

Resources:
# See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Expand All @@ -40,3 +63,7 @@ Resources:
Environment:
Variables:
RS_SECRET_ARN: !Ref RedshiftSecretArn
RS_CLUSTER_ID: !Ref RedshiftClusterId
RS_CLUSTER_REGION: !Ref "AWS::Region"
RS_CLUSTER_DATABASE: !Ref RedshiftClusterDatabase
SQL_STATEMENT: !Ref SQLStatement

0 comments on commit 8cea3db

Please sign in to comment.