CodePipeline for a Docker image of the Micronaut application GraalVM Native Image

Continuous deployment - CodePipeline: Github → CodeBuild → GraalVM Native Image → ECR → Elastic Beanstalk

Micronaut is build agnostic, you can use either Gradle or Maven. However, Gradle is my build tool of choice.

Changes to Gradle Build


Create a template for

A file describes how to deploy a remote Docker image as an Elastic Beanstalk application. This JSON file is specific to Elastic Beanstalk. If your application runs on an image that is available in a hosted repository, you can specify the image in a Dockerrun.

    "AWSEBDockerrunVersion": "1",
    "Image": {
        "Name": "",
        "Update": "true"
    "Ports": [
            "ContainerPort": "5000"

I define the properties in


Create a Gradle tasks to populate the JSON template:


task copyAndReplaceTokensFromDockerrun(type: Copy) {
    from('') {
        include ''
        filter(ReplaceTokens, tokens: [imageTag: projectVersion,
                                       awsAccountId: awsAccountId,
                                       awsRegion: awsRegion,
                                       awsEcrRepositoryName: awsEcrRepositoryName])
    into 'build/docker'

Configure Container registry

Configure the Amazon ECR repository as the container registry:


version = projectVersion


dockerBuildNative {
  images = ["${awsAccountId}.dkr.ecr.${awsRegion}${awsEcrRepositoryName}:$project.version"]

AWS CodeBuild

When you create your CodeBuild project, in the environment section, check:

[x] Enable this flag if you want to build Docker images or want your builds to get elevated privileges`

CodeBuild Memory

GraalVM Native Image generation is a memory intensive operation.

Select 7 GB memory, 4 vCPUs instead of the default 3 GB memory, 2 vCPUs.


Create buildspec.yml file:

A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build.

version: 0.2

      java: corretto11 
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
      - ./gradlew dockerPushNative
      - ./gradlew copyAndReplaceTokensFromDockerrun
      - rm -f  /root/.gradle/caches/modules-2/modules-2.lock
      - rm -fr /root/.gradle/caches/*/plugin-resolution/
    - ''
  base-directory: 'build/docker'
    - '/root/.gradle/caches/**/*' 
    - '/root/.gradle/wrapper/**/*'
  • dockerPushNative, a task provided by the Micronaut Gradle Plugin, pushes a Docker Image built with GraalVM Native Image of the Micronaut application to ECR, the configured container registry

The build's artifact is the output of the Gradle copyAndReplaceTokensFromDockerrun task.

AWS Code Build IAM Role

Attach a policy to the IAM role used by AWS CodeBuild to allow the build to publishing a new image to a Amazon ECR repository. For example, the AWS Managed policy AmazonEC2ContainerRegistryPowerUser.

Elastic Beanstalk

Create a Web server environment. As platform select Docker running on a 64bit Amazon Linux 2.

AWS Elastic Beanstalk IAM Role

Attach a policy to the EC2 IAM role used by AWS Elastic Beanstalk to allow the environment to retrieve the image from Amazon ECR repository. For example, the AWS Managed policy AmazonEC2ContainerRegistryReadOnly.

Start Micronaut Application in port 5000

Set environment variable MICRONAUT_SERVER_PORT with value 5000 in AWS Elastic Beanstalk Configuration → Software.

AWS CodePipeline

AWS CodePipeline:

  • Source: Github (Version 2)
  • Build Provider: AWS CodeBuild
  • Deploy Provider: AWS Elastic Beanstalk

Tags: #micronaut #aws #ecr #codebuild #elasticbeanstalk #graalvm #codebuild #codepipeline #docker
May 2021, 25.


My next events:
🗓 Apr 04 16:30 JDevSummitIL Getting Started with the Micronaut Framework