-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfn-build.yml
162 lines (132 loc) · 6.53 KB
/
cfn-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
AWSTemplateFormatVersion: 2010-09-09
Resources:
LambdaJavaBaseImageRepository:
Type: AWS::ECR::PublicRepository
Properties:
RepositoryName: lambda/java
RepositoryCatalogData:
AboutText: |+
Unofficial community base image for Lambda that contains all the required components to
run your functions packaged as container images on AWS Lambda. This base image contains
the Amazon Linux Base operating system, the runtime for Java, dependencies and the Lambda
Runtime Interface Client (RIC), which implements the
[Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html).
The Lambda Runtime Interface Client allows your runtime to receive requests from and send
requests to the Lambda service.
To learn more about the composition of this base image you can visit
[aleph0io/aws-lambda-java-base-images](https://github.com/aleph0io/aws-lambda-java-base-images)
on GitHub.
Maintained by [@sigpwned](https://twitter.com/sigpwned). Drop me a line!
## Maintenance policy
These images will be updated periodically to pick up new security patches. The components
and dependencies included in the image may change from time to time, but they will always
work for deploying images to AWS Lambda.
UsageText: |-
You can find an entire example AWS Lambda function implementation on GitHub at
[aleph0io/example-java-lambda-function](https://github.com/aleph0io/example-java-lambda-function).
Otherwise, you can get started by using these images in your Dockerfile and coping your
class files into the `/var/task` folder in your image. The runtime jar dependencies should
be copied into `/var/task/lib` directory.
```
FROM public.ecr.aws/aleph0io/lambda/java:17
# Copy function code and runtime dependencies from Gradle layout
COPY build/classes/java/main ${LAMBDA_TASK_ROOT}
COPY build/dependency/* ${LAMBDA_TASK_ROOT}/lib/
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "com.example.LambdaHandler::handleRequest" ]
```
Example Gradle task definition to prepare the runtime dependencies:
```
task copyRuntimeDependencies(type: Copy) {
from configurations.runtimeClasspath
into 'build/dependency'
}
build.dependsOn copyRuntimeDependencies
```
If you are using Maven, adjust COPY command in the Dockerfile accordingly:
```
FROM public.ecr.aws/aleph0io/lambda/java:17
# Copy function code and runtime dependencies from Maven layout
COPY target/classes ${LAMBDA_TASK_ROOT}
COPY target/dependency/* ${LAMBDA_TASK_ROOT}/lib/
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "com.example.LambdaHandler::handleRequest" ]
```
You can use maven-dependency-plugin to collect runtime dependencies:
```
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- Configuration goes here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
Use `mvn compile dependency:copy-dependencies -DincludeScope=runtime` to compile the
project and collect the runtime dependencies.
The resulting layout should look like:
/var/task/
├── com
│ └── example
│ ├── LambdaHandler.class
│ └── <your other class files here>
└── lib
├── aws-lambda-java-core-1.2.1.jar
├── aws-lambda-java-events-3.7.0.jar
└── <your other JAR dependencies here>
You can then locally test your function using the docker build and docker run commands.
To build your image:
docker build -t <image name> .
To run your image locally:
docker run -p 9000:8080 <image name>
In a separate terminal, you can then locally invoke the function using cURL:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'
RepositoryDescription: >
Unofficial Community AWS Lambda Base Images for Java 17+
OperatingSystems:
- Linux
Architectures:
- x86-64
Tags:
- Key: aleph0
Value: true
- Key: aleph0:scope
Value: public
- Key: aleph0:role
Value: lambda
Outputs:
JavaBaseImageRepositoryImageArn:
Description: The ARN of the Java base image repository
Value: !GetAtt LambdaJavaBaseImageRepository.Arn
Export:
Name: "lambda-java-base-image-arn"