[JAVA] I built Step Functions with AWS CDK.

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!

environment

What i did

I recently created Step Functions in my work, so We built StepFunctions on AWS CDK.

Contents

cdk command installation

npm install -g aws-cdk

Create project template

You can generate a project template for Java with the following command.

cdk init --a {project name} --language java

スクリーンショット 2019-12-07 16.35.36.png

Maven project. I personally miss it. Here, we set project name = sample.

App and Stack

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.

スクリーンショット 2019-12-07 16.52.06.png

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.

スクリーンショット 2019-12-07 16.52.54.png

Building Step Functions

Delete the default SQS and SNS, Add Lambda and Step Functions to Stack.

Add dependency

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 resources to Stack

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();
    }
}

Lambda file storage

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.

スクリーンショット 2019-12-07 17.42.58.png

Deploy

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.

スクリーンショット 2019-12-07 17.53.17.png

resource

Step Functions was created successfully I was able to do it!

スクリーンショット 2019-12-07 17.53.17.png スクリーンショット 2019-12-07 17.53.36.png

Impressions

Hands Lab Advent Calendar 2019, The 8th day is @watarukura!

I used it as a reference

Recommended Posts

I built Step Functions with AWS CDK.
I built a Code Pipeline with AWS CDK.
I dealt with Azure Functions not working in Java
I played with Refinements
I tried automatic deployment with CircleCI + Capistrano + AWS (EC2) + Rails
I want to implement various functions with kotlin and java!
[AWS] What I'm glad I did with my first deployment