Skip to content

Commit 514c42a

Browse files
committed
Support Lambda
1 parent 868748d commit 514c42a

16 files changed

+877
-47
lines changed

README.md

+152-35
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,52 @@ Current Features / Supported AWS Products
77
-----------------------------------------
88

99
* S3
10-
* Create or delete buckets
11-
* Upload or delete objects
10+
* Create bucket
11+
* Delete bucket
12+
* Upload object(s)
13+
* Delete object(s)
1214
* File sync
1315
* EC2
16+
* Run instance
1417
* Start instance
1518
* Stop instance
16-
* Authorize security group permissions
19+
* Terminate instance
20+
* Import key
21+
* Authorize security group ingress permissions
22+
* Authorize security group egress permissions
23+
* Revoke security group ingress permissions
24+
* Revoke security group egress permissions
1725
* Wait instance for specific status
18-
* ELB
19-
* (TBD)
26+
* RDS
27+
* Create DB instance
28+
* Delete DB instance
29+
* Modify DB instance
30+
* Migrate (create or modify) DB instance
31+
* Reboot DB instance
32+
* Wait DB instance for specific status
2033
* Route53
21-
* Create or delete hosted zones
22-
* Change record sets
23-
* CloudFormation
24-
* Migrate (create or update) stacks
25-
* Delete stacks
26-
* Wait stack for specific status
34+
* Create hosted zone
35+
* Delete hosted zone
36+
* Change record set
2737
* Elastic Beanstalk
2838
* Create or delete applications
2939
* Create or terminate environments
3040
* Create or delete configuration templates
3141
* Create or delete application versions
3242
* Wait environment for specific status
43+
* CloudFormation
44+
* Migrate (create or update) stack
45+
* Delete stack
46+
* Wait stack for specific status
47+
* Lambda
48+
* Create function
49+
* Update function code
50+
* Update function configuration
51+
* Migrate (create or update) function
52+
* Invoke function
53+
* Delete function
54+
* ELB
55+
* (TBD)
3356

3457
Requirements
3558
------------
@@ -64,7 +87,21 @@ aws {
6487
These credentials are used to make API accesses by default.
6588

6689

67-
### Implements tasks to start and stop bastion instance
90+
### S3 files tasks
91+
92+
```
93+
apply plugin: 'aws-s3'
94+
95+
task syncObjects(type: jp.classmethod.aws.gradle.s3.SyncTask) {
96+
bucketName 'foobar.example.com'
97+
source file('path/to/objects')
98+
}
99+
```
100+
101+
Look [S3 example 1](samples/01-s3-upload-simple) and [S3 example 2](samples/02-s3-sync-contents) for more information.
102+
103+
104+
### EC2 instance tasks
68105

69106
```
70107
apply plugin: 'aws-ec2'
@@ -84,38 +121,47 @@ task startBastion(type: jp.classmethod.aws.gradle.ec2.AmazonEC2StartInstanceTask
84121
}
85122
```
86123

87-
### Implements sync S3 files task
124+
Look [EC2 example](samples/03-ec2) for more information.
88125

89-
```
90-
apply plugin: 'aws-s3'
91126

92-
task syncObjects(type: jp.classmethod.aws.gradle.s3.SyncTask) {
93-
bucketName 'foobar.example.com'
94-
source file('path/to/objects')
95-
}
127+
### RDS DB instance tasks
128+
96129
```
130+
apply plugin: "aws-rds"
97131
98-
### Implements tasks to migrate and delete stack
132+
// You can overwrite default credentials and region settings like this:
133+
// rds {
134+
// profileName 'another-credentials-profile-name' // optional
135+
// region = 'us-east-1'
136+
// }
99137
100-
```
101-
apply plugin: 'aws-cloudformation'
138+
task migrateDBInstance(type: AmazonRDSMigrateDBInstanceTask) {
139+
dbInstanceIdentifier = "foobar"
140+
allocatedStorage = 5
141+
dbInstanceClass = "db.t2.micro"
142+
engine = "MySQL"
143+
masterUsername = "root"
144+
masterUserPassword = "passW0rd"
145+
vpcSecurityGroupIds = [ "sg-d3958fbf" ]
146+
dbSubnetGroupName = "default"
147+
multiAZ = false
148+
publiclyAccessible = true
149+
}
102150
103-
cloudFormation {
104-
stackName 'foobar-stack'
105-
stackParams([
106-
Foo: 'bar',
107-
Baz: 'qux'
108-
])
109-
capabilityIam true
110-
templateFile project.file("foobar.template")
111-
templateBucket 'example-bucket'
112-
templateKeyPrefix 'foobar/'
151+
task rebootDBInstance(type: AmazonRDSRebootDBInstanceTask) {
152+
dbInstanceIdentifier = "foobar"
113153
}
114154
115-
// awsCfnMigrateStack and awsCfnDeleteStack task (and so on) is declared.
155+
task deleteDBInstance(type: AmazonRDSDeleteDBInstanceTask) {
156+
dbInstanceIdentifier = "foobar"
157+
skipFinalSnapshot = true
158+
}
116159
```
117160

118-
### Implemets create / delete hosted zone task
161+
Look [RDS example](samples/07-rds) for more information.
162+
163+
164+
### Route 53 hosted zone tasks
119165

120166
```
121167
apply plugin: 'aws-route53'
@@ -130,7 +176,10 @@ task deleteHostedZone(type: jp.classmethod.aws.gradle.route53.DeleteHostedZoneTa
130176
}
131177
```
132178

133-
### Implements tasks to manage Elastic Beanstalk environemnt
179+
Look [Route 53 example](samples/04-route53) for more information.
180+
181+
182+
### Elastic Beanstalk environemnt tasks
134183

135184
```
136185
apply plugin: 'aws-beanstalk'
@@ -170,6 +219,74 @@ beanstalk {
170219
// task awsEbMigrateEnvironment, awsEbDeleteApplication and so on are declared
171220
```
172221

222+
Look [Elastic Beanstalk example](samples/05-beanstalk) for more information.
223+
224+
225+
### CloudFormation stack tasks
226+
227+
```
228+
apply plugin: 'aws-cloudformation'
229+
230+
cloudFormation {
231+
stackName 'foobar-stack'
232+
stackParams([
233+
Foo: 'bar',
234+
Baz: 'qux'
235+
])
236+
capabilityIam true
237+
templateFile project.file("foobar.template")
238+
templateBucket 'example-bucket'
239+
templateKeyPrefix 'foobar/'
240+
}
241+
242+
// awsCfnMigrateStack and awsCfnDeleteStack task (and so on) is declared.
243+
```
244+
245+
Look [CloudFormation example](samples/06-cloudformation) for more information.
246+
247+
248+
### Lambda function tasks
249+
250+
```
251+
apply plugin:'base'
252+
apply plugin: "aws-lambda"
253+
aws {
254+
profileName "default"
255+
region "ap-northeast-1"
256+
}
257+
258+
lambda {
259+
region "us-east-1"
260+
}
261+
262+
task zip(type: Zip) {
263+
from "function/"
264+
destinationDir file("build")
265+
}
266+
267+
task migrateFunction(type: AWSLambdaMigrateFunctionTask, dependsOn: zip) {
268+
functionName = "foobar"
269+
role = "arn:aws:iam::${aws.accountId}:role/lambda-poweruser"
270+
zipFile = zip.archivePath
271+
handler = "DecodeBase64.handler"
272+
}
273+
274+
task invokeFunction(type: AWSLambdaInvokeTask) {
275+
functionName = "foobar"
276+
invocationType = InvocationType.RequestResponse
277+
payload = file("sample-input/input.txt")
278+
doLast {
279+
println "Lambda function result: " + new String(invokeResult.payload.array(), "UTF-8")
280+
}
281+
}
282+
283+
task deleteFunction(type: AWSLambdaDeleteFunctionTask) {
284+
functionName = "foobar"
285+
}
286+
```
287+
288+
Look [Lambda example](samples/08-lambda) for more information.
289+
173290

174291
License
175292
-------

samples/07-rds/build.gradle

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ aws {
2222
region "ap-northeast-1"
2323
}
2424

25-
if (hasProperty('instanceId') == false) { ext.instanceId = 'i-12345678' }
26-
2725
task migrateDBInstance(type: AmazonRDSMigrateDBInstanceTask) {
28-
dbInstanceIdentifier = "hogex"
26+
dbInstanceIdentifier = "foobar"
2927
allocatedStorage = 5
3028
dbInstanceClass = "db.t2.micro"
3129
engine = "MySQL"
@@ -38,16 +36,16 @@ task migrateDBInstance(type: AmazonRDSMigrateDBInstanceTask) {
3836
}
3937

4038
task rebootDBInstance(type: AmazonRDSRebootDBInstanceTask) {
41-
dbInstanceIdentifier = "hogex"
39+
dbInstanceIdentifier = "foobar"
4240
}
4341

4442
task deleteDBInstance(type: AmazonRDSDeleteDBInstanceTask) {
45-
dbInstanceIdentifier = "hogex"
43+
dbInstanceIdentifier = "foobar"
4644
skipFinalSnapshot = true
4745
}
4846

4947
task waitDBInstanceStatus(type: AmazonRDSWaitInstanceStatusTask) {
50-
dbInstanceIdentifier = "hogex"
48+
dbInstanceIdentifier = "foobar"
5149
successStatuses = [ "available", "terminated" ]
5250
waitStatuses = [
5351
"backing-up",

samples/08-lambda/build.gradle

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// -*- coding: utf-8; mode: groovy -*-
2+
3+
import com.amazonaws.services.lambda.model.InvocationType;
4+
5+
import jp.classmethod.aws.gradle.lambda.AWSLambdaDeleteFunctionTask;
6+
import jp.classmethod.aws.gradle.lambda.AWSLambdaInvokeTask;
7+
import jp.classmethod.aws.gradle.lambda.AWSLambdaMigrateFunctionTask;
8+
9+
buildscript {
10+
repositories {
11+
mavenCentral()
12+
maven { url "http://public-maven.classmethod.info/release" }
13+
maven { url "http://public-maven.classmethod.info/snapshot" }
14+
}
15+
dependencies {
16+
classpath "jp.classmethod.aws:gradle-aws-plugin:0.+"
17+
}
18+
}
19+
20+
apply plugin:'base'
21+
apply plugin: "aws-lambda"
22+
aws {
23+
profileName "default"
24+
region "ap-northeast-1"
25+
}
26+
27+
lambda {
28+
region "us-east-1"
29+
}
30+
31+
task zip(type: Zip) {
32+
from "function/"
33+
destinationDir file("build")
34+
}
35+
36+
task migrateFunction(type: AWSLambdaMigrateFunctionTask, dependsOn: zip) {
37+
functionName = "foobar"
38+
role = "arn:aws:iam::${aws.accountId}:role/lambda-poweruser"
39+
zipFile = zip.archivePath
40+
handler = "DecodeBase64.handler"
41+
}
42+
43+
task invokeFunction(type: AWSLambdaInvokeTask) {
44+
functionName = "foobar"
45+
invocationType = InvocationType.RequestResponse
46+
payload = file("sample-input/input.txt")
47+
doLast {
48+
println "Lambda function result: " + new String(invokeResult.payload.array(), "UTF-8")
49+
}
50+
}
51+
52+
task deleteFunction(type: AWSLambdaDeleteFunctionTask) {
53+
functionName = "foobar"
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log('Loading function');
2+
3+
exports.handler = function(event, context) {
4+
console.log(JSON.stringify(event, null, 2));
5+
var payload = new Buffer(event.data, 'base64').toString('ascii');
6+
context.succeed(payload);
7+
};

samples/08-lambda/outputfile.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
null
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4="
3+
}

src/main/java/jp/classmethod/aws/gradle/elasticbeanstalk/EbConfigurationTemplateExtension.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44

55
import java.io.File;
66
import java.io.IOException;
7-
import java.nio.file.Files;
87

98
import lombok.Getter;
109
import lombok.Setter;
1110

1211
import org.gradle.api.Named;
1312

1413
import com.google.common.base.Charsets;
14+
import com.google.common.io.Files;
1515

1616
public class EbConfigurationTemplateExtension implements Named {
1717

18-
static String readFile(File file) throws IOException {
19-
return new String(Files.readAllBytes(file.toPath()), Charsets.UTF_8);
20-
}
21-
2218

2319
@Getter
2420
@Setter
@@ -51,7 +47,7 @@ public String getOptionSettings() throws IOException {
5147
}
5248
if (optionSettings instanceof File) {
5349
File file = (File) optionSettings;
54-
return readFile(file);
50+
return Files.toString(file, Charsets.UTF_8);
5551
}
5652
return optionSettings.toString();
5753
}

0 commit comments

Comments
 (0)