Minimum configuration sample to automatically release Lambda by Java with Code pipeline

TL;DR

The following document provides an example of automating the release of a Lambda function in node.js. http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/automating-deployment.html

In this document, we will write an app similar to this in Java and automate its release. Minimize the explanation of the meaning and role of each element. (I'm not sure. I'm sorry.) I will give an example of the minimum operation sample.

In this document, various settings are manually entered on the management console. The procedure may have changed due to future UI changes. Please forgive me.

Premise

--The code is placed on github. --You have a branch to detect updates. --In this example, assume that you have the master branch at the following URL: https://github.com/kazurof/minimum-java-lambda-with-codepipeline Sample code is also located here. --Build with Gradle. --There is an S3 bucket to put the intermediate files. As an example, let's say ʻauto-release-sample`.

Preparation

Creating a role

Open the IAM Management Console and create a role as shown below.

item value Remarks
name cloudformation-lambda-execution-role
Roll type AWS CloudFormation When creating, select from "AWS Service Role". On the created role screen, it will be displayed on the Trusts tab
Attached policy AWSLambdaExecute On the role screen, it appears in the Permissions tab => Administrative Policy

After creating the role once, open the created role in the management console and click Add Inline Policy. After proceeding to Custom Policy-> Select, enter the following contents. Set the policy name as appropriate.

{
    "Statement": [
        {
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:GetBucketVersioning"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::codepipeline*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "lambda:*"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "iam:GetRole",
                "iam:CreateRole",
                "iam:DeleteRole"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "iam:AttachRolePolicy",
                "iam:DetachRolePolicy"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "iam:PassRole"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "cloudformation:CreateChangeSet"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        }
    ],
    "Version": "2012-10-17"
}

Note: You may not have the minimum required permissions. I would like to reduce it as much as possible in future updates of this article.

Place the file on github

Save the application source code on github.

Please refer to the sample repository for the specific contents of the file. https://github.com/kazurof/minimum-java-lambda-with-codepipeline Describe the directory structure of the file.

│  build.gradle
│  buildspec.yml
│  gradlew
│  gradlew.bat
│  minimum-lambda-java-model.yaml
│
│
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│
└─src
    └─main
        └─java
            └─codepipelinesample
                    Main.java

Supplement

buildspec.yml contains the name of the S3 repository to use. (First, ʻauto-release-sample`.) If you want to actually run it, change it to the repository name you prepared.

Creating a CodePipeline

Open the AWS CodePipeline management console and create the pipeline as follows.

Pipeline name

Anything you know about the pipeline name will do.

Source

item value Remarks
Source provider GitHub If you select GitHub, specify the URL and branch of the GitHub repository, perform OAuth authentication, and enable access to the specified GitHub repository from CodePipeline.
Action category Source You will not be prompted for input when creating. Anything that can be attached automatically is OK
Action name Source You will not be prompted for input when creating. You can use the name that is automatically assigned.
Output artifact name MyApp You will not be prompted for input when creating. You can use the name that is automatically assigned.

Build

item value Remarks
Build provider AWS CodeBuild
Action category Build You will not be prompted for input when creating.
Action name CodeBuild You will not be prompted for input when creating.

Select Create a new CodeBuild project. Anything is fine as long as you know the project name.

Creating a build project

item value
Build environment Ubuntu Java8
Build specifications Buildspec in the root directory of the source code.use yml

note: --At this time, the AWS CodeBuild service role is automatically created on your behalf. It will be in the form of code-build-<build project name> -service-role. --In the action of this build, the following items are automatically set as follows.

item value
Input artifact MyApp
Output artifact MyAppBuild

Click "Save Build Project" and then "Next Step".

Deploy

item value Remarks
Deployment provider AWS CloudFormation
Action mode Create or replace change sets
The name of the stack MyBetaStack Also used for action names.
Change set name MyChangeSet
Template file packaged-minimum-lambda-java-model.yaml
Capabilities CAPABILITY_IAM
Role name cloudformation-lambda-execution-role Specify the role created at the beginning of this procedure.
Action category Deploy Set automatically

Creating an AWS service role

Make a new roll.

Reprinted the wording on the screen: "Create a service role in IAM for AWS CodePipeline to grant permissions to use resources in your account."

--Click "Create Role". --Press "Allow". --Click "Next Step".

The role name should be "AWS-CodePipeline-Service". (If it already exists, it will be added to the inline policy. It is unconfirmed.)

Checking the pipeline

A confirmation screen of the contents entered so far is displayed. Click "Create Pipeline".

Fix service role

Modify the service role created for Codebuild so that it can access the prepared S3 bucket (ʻauto-release-sample`).

In the procedure so far, the role code-build- <build project name> -service-role has been generated, so modify it.

--From the IAM Management Console, select a role. --Select code-build-<build project name> -service-role. --"Access authority" tab => Click "Add inline policy". --Select "Policy Generator" and select "Select". --In "AWS Service", select "Amazon S3". --In "Actions", select "PutObject". --Enter ʻarn: aws: s3 ::: auto-release-sample * `in Amazon Resource Name (ARN). (Asterisk at the end is required!) --Click "Add Statement" and then "Next Step". --Click "Apply Policy".

Add a step to update Lambda to CodePipeline

--Go to AWS CodePipeline Management Console => Select the created pipeline --Click the edit button --Click the Staging pencil icon --Select the [+ Action] icon at the end of an existing action. --Enter as follows

item value
Action category Deploy
Action name execute_cs (Anything is fine if you know)
Deployment provider AWS CloudFormation
Action mode Run change set
The name of the stack MyBetaStack
Change set name MyChangeSet

--Press "Update" to save --Press "Save Pipeline Changes" to save the entire pipeline

Automatic release and running Lambda

This completes Codepipeline creation. Try making some modifications to the master branch of your git repository. You can see where the pipeline works in the Codepipeline management console.

When it finishes successfully, open the Lambda management console. You should have created a Java Lambda function. You can run the test.

Impressions

The procedure is long, probably because there are many elemental technologies involved. .. .. : sweat_smile:

Recommended Posts

Minimum configuration sample to automatically release Lambda by Java with Code pipeline
How to manage Java code automatically generated by jOOQ & Flyway sample
Sample code to parse date and time with Java SimpleDateFormat
Tips around the sample to release Java Lambda on CodePipeline
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 2
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 1
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 3
Java sample code 02
Java sample code 03
How to use Java framework with AWS Lambda! ??
[Java] Explanation of Strategy pattern (with sample code)
Java sample code 04
How to use Java API with lambda expression
Java sample code 01
Sample code to convert List to List <String> in Java Stream
How to deploy Java to AWS Lambda with Serverless Framework
How to build Java development environment with VS Code
Sample code for log output by Java + SLF4J + Logback
How to decompile apk file to java source code with MAC
A memo to start Java programming with VS Code (2020-04 version)
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
Use Lambda Layers with Java
Digital signature sample code (JAVA)
Java to play with Function
Java parallelization code sample collection
[Java] Introduction to lambda expressions
Connect to MySQL 8 with Java
Is it possible to automatically generate Getters / Setters with Java Interface?
Try Hello World with the minimum configuration of Heroku Java spring-boot
Settings to delete unused Java imports when saving with VS Code
Sample code to unit test a Spring Boot controller with MockMvc
[With sample code] Basics of Spring JDBC to learn with Blog app
Sample code to call the Yahoo! Local Search API in Java
Sample code to call Yahoo! Shopping Product Search (v3) API with HTTP Client API officially introduced from Java 11
[Code Pipeline x Elastic Beanstalk] Summary of errors and countermeasures for CI / CD Java applications to Elastic Beanstalk with Code Pipeline