This article is the 7th day article of Hands Lab Advent Calendar 2019.
Good work. @naokiur. I will be in charge of this year as well.
Recently, when building AWS resources in business, With CloudFormation and Serverless I try to code it. (Keeping the dosage and remembering the purpose ...)
Thanks to that, I'm getting used to writing YAML format files.
Meanwhile, Knowing the existence of AWS CDK What's more, AWS CDK makes it generally available for Java and .NET! !!
I personally like Java, I tried AWS CDK in Java.
Overview I was also recommended, CDK Workshop published by AWS CDK It was really good for me who didn't do anything!
I recently created Step Functions in my work, so We built StepFunctions on AWS CDK.
npm install -g aws-cdk
You can generate a project template for Java with the following command.
cdk init --a {project name} --language java
Maven project.
I personally miss it.
Here, we set project name = sample
.
When you generate a project template,
SampleApp
and SampleStack
have been generated.
Stack is, as the name suggests This will be 1 Stack of CloudFormation, I am aware that.
By default It seems that it is a Stack that creates SQS and SNS.
pom.xml
Apart from core
Dependencies are listed for each AWS service.
If you use more AWS services, it seems that you need to add dependencies.
Delete the default SQS and SNS, Add Lambda and Step Functions to Stack.
Add the Lambda and StepFunctions libraries to pom.xml.
pom.xml
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>lambda</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>stepfunctions</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>stepfunctions-tasks</artifactId>
<version>1.18.0</version>
</dependency>
Regarding StepFunctions
Not just stepfunctions
You need to add stepfunctions-tasks
.
Add Lambda and Step Functions resources.
In the form of {Resource} .Builder.hoge (). Fuga () .build ()
It seems that it is often possible to generate resources.
It's my personal preference, but I feel that the description is complete and easy to read.
SampleStack.java
public class SampleStack extends Stack {
public SampleStack(final Construct parent, final String id) {
this(parent, id, null);
}
public SampleStack(final Construct parent, final String id, final StackProps props) {
super(parent, id, props);
final Function hello = Function.Builder.create(this, "HelloHandler")
.runtime(Runtime.PYTHON_3_6)
.code(Code.fromAsset("lambda"))
.handler("hello.lambda_handler")
.build();
final Function world = Function.Builder.create(this, "WorldHandler")
.runtime(Runtime.PYTHON_3_6)
.code(Code.fromAsset("lambda"))
.handler("world.lambda_handler")
.build();
final Task helloTask = Task.Builder.create(this, "HelloTask")
.task(InvokeFunction.Builder.create(hello).build())
.build();
final Task worldTask = Task.Builder.create(this, "WorldTask")
.task(InvokeFunction.Builder.create(world).build())
.build();
final StateMachine machine = StateMachine.Builder.create(this, "SampleStateMachine")
.definition(helloTask.next(worldTask))
.build();
}
}
The method that actually works on Lambda is
It seems that it needs to be stored under the directory in the same row as the src directory
.
The method that actually runs on Lambda does not have to be Java
, so
I prepared a Python file here.
Since it was not executed, first execute the following command.
cdk bootstrap
It will create a Stack to create an S3 bucket for deployment.
For deployment Execute the following command.
mvn package
cdk deploy
By the above, You have created a Stack on CloudFormation.
Step Functions was created successfully I was able to do it!
not at all
compile error
.Hands Lab Advent Calendar 2019, The 8th day is @watarukura!
Recommended Posts