Skip to content
Permalink
Browse files

Added lambda code example

  • Loading branch information...
wazcov committed Apr 16, 2019
1 parent 6ba026d commit a057dd4b793d57b2b0700484f26c0510bc4d3870
10 pom.xml
@@ -64,6 +64,16 @@
<version>1.0.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.534</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-rds</artifactId>
@@ -1,4 +1,4 @@
package com.waynecovell.awsJavaSamples.AwsShared;
package com.wazcov.awsJavaSamples.AwsShared;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
@@ -0,0 +1,40 @@
package com.wazcov.awsJavaSamples.V1SDK.Lambda;

import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;

import java.net.URLDecoder;

/*
This is an example function for processing data from an S3 Bucket in AWS Lambda.
1) Upload a .jar file containing a class like this one to AWS Lambda.
2) Set an S3 Trigger of "Event type: ObjectCreated" TODO: Add infrastructure code to build this
3) Set the Handler to match "com.wazcov.awsJavaSamples.V1SDK.Lambda.Lambda_Start::testCode"
4) Upload a file with text in it to S3
5) Check CloudWatch logs for the System.out statements
*/

public class Lambda_Start {
public static void testCode(S3Event s3Event) throws Exception {

StringBuilder stringBuilder = new StringBuilder();

s3Event.getRecords().forEach(e -> {
try {
String bucket = e.getS3().getBucket().getName();

String key = e.getS3().getObject().getKey().replace('+', ' ');
key = URLDecoder.decode(key, "UTF-8");

AmazonS3 s3Client = AmazonS3Client.builder().build();
s3Client.getObjectAsString(bucket, key);
stringBuilder.append(s3Client.getObjectAsString(bucket, key));

} catch (Exception ex) {
System.out.println(ex.toString());
}
});
}
}
@@ -1,6 +1,6 @@
package com.waynecovell.awsJavaSamples.V1SDK.RDS;
package com.wazcov.awsJavaSamples.V1SDK.RDS;

import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.rds.AmazonRDS;
import com.amazonaws.services.rds.AmazonRDSClientBuilder;
@@ -1,6 +1,6 @@
package com.waynecovell.awsJavaSamples.V1SDK.SES;
package com.wazcov.awsJavaSamples.V1SDK.SES;

import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
@@ -1,4 +1,4 @@
package com.waynecovell.awsJavaSamples.V1SDK.SM;
package com.wazcov.awsJavaSamples.V1SDK.SM;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
@@ -9,7 +9,7 @@
import com.amazonaws.services.secretsmanager.model.ListSecretsRequest;
import com.amazonaws.services.secretsmanager.model.ListSecretsResult;
import com.amazonaws.util.IOUtils;
import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;

import java.io.InputStream;
import java.nio.ByteBuffer;
@@ -1,6 +1,6 @@
package com.waynecovell.awsJavaSamples.V1SDK.SNS;
package com.wazcov.awsJavaSamples.V1SDK.SNS;

import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNS;
@@ -1,6 +1,6 @@
package com.waynecovell.awsJavaSamples.V1SDK.SQS;
package com.wazcov.awsJavaSamples.V1SDK.SQS;

import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.amazon.sqs.javamessaging.ProviderConfiguration;
import com.amazon.sqs.javamessaging.SQSConnection;
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
@@ -1,7 +1,7 @@
package com.waynecovell.awsJavaSamples.V2SDK.SM;
package com.wazcov.awsJavaSamples.V2SDK.SM;

import com.amazonaws.util.IOUtils;
import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
@@ -1,4 +1,4 @@
import com.waynecovell.awsJavaSamples.AwsShared.AWSSharedUtils;
import com.wazcov.awsJavaSamples.AwsShared.AWSSharedUtils;
import org.junit.Test;

import static org.junit.Assert.assertNotEquals;

0 comments on commit a057dd4

Please sign in to comment.
You can’t perform that action at this time.