Skip to content

Commit e7cf64a

Browse files
committed
First commit
1 parent 938076f commit e7cf64a

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

awslambdacreate/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# AWS Lambda Function Create Java example
2+
3+
This folder contains a Java application example that handles Lambda functions on AWS (Amazon Web Services).
4+
5+
Create an AWS Lambda function.
6+
7+
## Requirements
8+
9+
* You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account.
10+
11+
* The code was written for:
12+
13+
* Java 8
14+
* Apache Maven 3
15+
* AWS SDK for Java (SDK V1)
16+
17+
## Using the code
18+
19+
* Configure your AWS access keys.
20+
21+
**Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access.
22+
23+
When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain.
24+
25+
Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches:
26+
27+
* The default credential profiles file
28+
29+
Set credentials in the AWS credentials profile file on your local system, located at:
30+
31+
`~/.aws/credentials` on Linux, macOS, or Unix
32+
33+
`C:\Users\USERNAME\.aws\credentials` on Windows
34+
35+
This file should contain lines in the following format:
36+
37+
```bash
38+
[default]
39+
aws_access_key_id = <your_access_key_id>
40+
aws_secret_access_key = <your_secret_access_key>
41+
```
42+
Substitute your own AWS credentials values for the values `<your_access_key_id>` and `<your_secret_access_key>`.
43+
44+
* Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
45+
46+
Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
47+
48+
To set these variables on Linux, macOS, or Unix, use `export`:
49+
50+
```bash
51+
export AWS_ACCESS_KEY_ID=<your_access_key_id>
52+
export AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
53+
```
54+
55+
To set these variables on Windows, use `set`:
56+
57+
```bash
58+
set AWS_ACCESS_KEY_ID=<your_access_key_id>
59+
set AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
60+
```
61+
62+
* You need a `JAR` or `ZIP` file where the code of the Lambda function is located.
63+
64+
You can use the code obtained from the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson).
65+
66+
* You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code.
67+
68+
* You have to create an AWS role that has Lambda permissions.
69+
70+
* Run the code.
71+
72+
You must provide 4 parameter:
73+
74+
* `<FUNCTION_NAME>` = Lambda function name
75+
* `<FUNCTION_FILE>` = The path to the JAR or ZIP file where the code of the Lambda function is located
76+
* `<FUNCTION_ROLE>` = The role ARN that has Lambda permissions
77+
* `<FUNCTION_HANDLER>` = The fully qualifed method name (Ex: example.Handler::handleRequest)
78+
79+
Run application:
80+
81+
```bash
82+
java -jar awslambdacreate.jar lambda-name lambda-file lambda-role lambda-handler
83+
```
84+
85+
You must use as a function role the ARN.
86+
87+
Ex.: `arn:aws:iam::123456789012:role/service-role/lambda-basic-execution`
88+
89+
* Test the application.
90+
91+
The Lambda function is created and you should see the message:
92+
93+
```bash
94+
Creating Lambda function ...
95+
Created
96+
```

awslambdacreate/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.alfonsof.awsexamples</groupId>
8+
<artifactId>aws-lambda-create</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<jdk.version>1.8</jdk.version>
14+
<maven.compiler.source>${jdk.version}</maven.compiler.source>
15+
<maven.compiler.target>${jdk.version}</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.amazonaws</groupId>
21+
<artifactId>aws-java-sdk-lambda</artifactId>
22+
<version>1.11.979</version>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.7.0</version>
32+
<configuration>
33+
<source>${jdk.version}</source>
34+
<target>${jdk.version}</target>
35+
</configuration>
36+
</plugin>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-shade-plugin</artifactId>
40+
<version>3.0.0</version>
41+
<configuration>
42+
<createDependencyReducedPom>false</createDependencyReducedPom>
43+
</configuration>
44+
<executions>
45+
<execution>
46+
<phase>package</phase>
47+
<goals>
48+
<goal>shade</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* LambdaCreate is an example that handles Lambda functions on AWS.
3+
* Create a Lambda function.
4+
* You must provide 1 parameter:
5+
* FUNCTION_NAME = Lambda function name
6+
* FUNCTION_FILE = The path to the JAR or ZIP file where the code of the Lambda function is located
7+
* FUNCTION_ROLE = The role ARN that has Lambda permissions
8+
* FUNCTION_HANDLER = The fully qualified method name (Ex: example.Handler::handleRequest)
9+
*/
10+
11+
package example;
12+
13+
import java.io.IOException;
14+
import java.nio.ByteBuffer;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
17+
import com.amazonaws.AmazonClientException;
18+
import com.amazonaws.AmazonServiceException;
19+
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
20+
import com.amazonaws.services.lambda.AWSLambda;
21+
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
22+
import com.amazonaws.services.lambda.model.ServiceException;
23+
import com.amazonaws.services.lambda.model.CreateFunctionRequest;
24+
import com.amazonaws.services.lambda.model.CreateFunctionResult;
25+
import com.amazonaws.services.lambda.model.FunctionCode;
26+
import com.amazonaws.services.lambda.model.VpcConfig;
27+
28+
29+
public class LambdaCreate {
30+
private static final String REGION = "eu-west-1"; // Region name
31+
32+
public static void main(String[] args) {
33+
34+
if (args.length < 4) {
35+
System.out.println("Not enough parameters.\n"+
36+
"Proper Usage is: java -jar lambdacreate.jar " +
37+
"<FUNCTION_NAME> <FUNCTION_FILE> <FUNCTION_ROLE> <FUNCTION_HANDLER>");
38+
System.exit(1);
39+
}
40+
41+
String functionName = args[0];
42+
String functionFile = args[1];
43+
String functionRole = args[2];
44+
String functionHandler = args[3];
45+
46+
System.out.println("Lambda function name: " + functionName);
47+
System.out.println("Lambda function file: " + functionFile);
48+
System.out.println("Lambda function role: " + functionRole);
49+
System.out.println("Lambda function handler: " + functionHandler);
50+
51+
AWSLambda awsLambda = AWSLambdaClientBuilder.standard()
52+
.withCredentials(new ProfileCredentialsProvider())
53+
.withRegion(REGION).build();
54+
55+
try {
56+
System.out.println("Creating Lambda function ...");
57+
58+
byte[] byteArray = Files.readAllBytes(Paths.get(functionFile));
59+
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
60+
61+
FunctionCode code = new FunctionCode().withZipFile(byteBuffer);
62+
63+
// Create Lambda function
64+
CreateFunctionRequest functionRequest = new CreateFunctionRequest()
65+
.withFunctionName(functionName)
66+
.withRuntime("java8")
67+
.withRole(functionRole)
68+
.withHandler(functionHandler)
69+
.withCode(code)
70+
.withDescription("Created by the Lambda Java API")
71+
.withTimeout(15)
72+
.withMemorySize(128)
73+
.withPublish(true)
74+
.withVpcConfig(new VpcConfig());
75+
CreateFunctionResult functionResponse = awsLambda.createFunction(functionRequest);
76+
77+
System.out.println("Created");
78+
System.out.println("The function ARN is " + functionResponse.getFunctionArn());
79+
80+
} catch (IOException ioe) {
81+
System.err.println(ioe.getMessage());
82+
} catch (ServiceException e) {
83+
System.out.println("ServiceException: " + e);
84+
} catch (AmazonServiceException ase) {
85+
System.out.println("Caught an AmazonServiceException, " +
86+
"which means your request made it " +
87+
"to AWS Lambda, but was rejected with an error " +
88+
"response for some reason.");
89+
System.out.println("Error Message: " + ase.getMessage());
90+
System.out.println("HTTP Status Code: " + ase.getStatusCode());
91+
System.out.println("AWS Error Code: " + ase.getErrorCode());
92+
System.out.println("Error Type: " + ase.getErrorType());
93+
System.out.println("Request ID: " + ase.getRequestId());
94+
} catch (AmazonClientException ace) {
95+
System.out.println("Caught an AmazonClientException, " +
96+
"which means the client encountered " +
97+
"an internal error while trying to " +
98+
" communicate with AWS Lambda, " +
99+
"such as not being able to access the network.");
100+
System.out.println("Error Message: " + ace.getMessage());
101+
}
102+
awsLambda.shutdown();
103+
}
104+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: example.LambdaCreate
3+

0 commit comments

Comments
 (0)