{"id":217,"date":"2023-03-17T14:47:22","date_gmt":"2023-03-17T14:47:22","guid":{"rendered":"https:\/\/keyshell.net\/blog\/?p=217"},"modified":"2023-03-17T14:47:22","modified_gmt":"2023-03-17T14:47:22","slug":"how-to-deploy-a-spring-boot-application-into-aws-ecs-via-github-actions","status":"publish","type":"post","link":"https:\/\/keyshell.net\/blog\/2023\/03\/17\/how-to-deploy-a-spring-boot-application-into-aws-ecs-via-github-actions\/","title":{"rendered":"How to deploy a spring-boot Application into AWS ECS via GitHub Actions"},"content":{"rendered":"<p>This blog describes deploying a spring-boot Application into AWS ESC via GitHub Actions. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS.<\/p>\n<p>Prerequisites:<br \/>\n1. AWS Account<br \/>\n2. GitHub Account<\/p>\n<p><strong>Create an AWS ECR repository<\/strong><\/p>\n<p><em>Login to your AWS account and create an Amazon Elastic Container Registry repository with a name.<\/em><\/p>\n<p><em>Create a cluster inside AWS ECS and select the cluster template as \u201cNetworking Only\u201d because we use AWS FARGATE here.<\/em><\/p>\n<p><strong>Create a Task Definition<\/strong><\/p>\n<p><em>Select launch type as FARGATE, select task role as \u201cecsTaskExecutionRole\u201d, Select your operating system, select Task memory and CPU, and Add container with container name and the image should be the ECR repository URI.<\/em><\/p>\n<p><strong>Create a service inside the cluster<\/strong><\/p>\n<p><em>Click on Clusters then select our cluster and create a service with a service name and select launch type as FARGATE.<\/em><\/p>\n<p><strong>Create a task-definition.json file on the Project repository<\/strong><\/p>\n<p><em>Go to the project repository, create a new file \u201ctask-definition.json\u201d and the file content will get from ( Task Definition &#8211;&gt; select your task &#8211;&gt; click on the Task name &#8211;&gt; Get a JSON file and copy the file content and paste it into the \u201ctask-definition.json\u201d file.)<\/em><\/p>\n<p><strong>Create the workflow<\/strong><\/p>\n<p><em>Add a YAML file to your repository (\/.github\/workflows\/filename.yml)<\/em><\/p>\n<pre><code>name: Build and Deploy to AWS\r\n\r\non:\r\n  push:\r\n    branches:\r\n      - main\r\n\r\njobs:\r\n  setup-build-publish-deploy:\r\n    name: Setup, Build, Publish, and Deploy\r\n    runs-on: ubuntu-latest\r\n\r\n    steps:\r\n      - name: Checkout Repository\r\n        uses: actions\/checkout@v2\r\n\r\n      # Setup JDK 1.8\r\n      - name: Set up JDK 1.8\r\n        uses: actions\/setup-java@v1\r\n        with:\r\n          java-version: 1.8\r\n          server-id: github\r\n          settings-path: ${{ github.workspace }}\r\n\r\n      # Build \r\n      - name: Build and Test with Maven\r\n        run: mvn -B package --file pom.xml\r\n\r\n      # Configure AWS credentials\r\n      - name: Configure AWS credentials\r\n        uses: aws-actions\/configure-aws-credentials@v1\r\n        with:\r\n          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}\r\n          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}\r\n          aws-region: ap-south-1\r\n\r\n      # Login to Amazon ECR\r\n      - name: Login to Amazon ECR\r\n        id: login-ecr\r\n        uses: aws-actions\/amazon-ecr-login@v1\r\n\r\n      # Build, tag, and push image to Amazon ECR\r\n      - name: Build, tag, and push image to Amazon ECR\r\n        id: build-image\r\n        env:\r\n          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}\r\n          ECR_REPOSITORY: springboot\r\n          IMAGE_TAG: develop\r\n        run: |\r\n          docker build -t $ECR_REGISTRY\/$ECR_REPOSITORY:$IMAGE_TAG .\r\n          docker push $ECR_REGISTRY\/$ECR_REPOSITORY:$IMAGE_TAG\r\n          echo \"::set-output name=image::$ECR_REGISTRY\/$ECR_REPOSITORY:$IMAGE_TAG\"\r\n      # Push the new image ID in the Amazon ECS task definition\r\n      - name: Push the new image ID in the Amazon ECS task definition\r\n        id: task-def\r\n        uses: aws-actions\/amazon-ecs-render-task-definition@v1\r\n        with:\r\n          task-definition: task-definition.json\r\n          container-name: springboot-container\r\n          image: ${{ steps.build-image.outputs.image }}\r\n\r\n      # Deploy Amazon ECS task definition\r\n      - name: Deploy Amazon ECS task definition\r\n        uses: aws-actions\/amazon-ecs-deploy-task-definition@v1\r\n        with:\r\n          task-definition: ${{ steps.task-def.outputs.task-definition }}\r\n          service: springboot-service\r\n          cluster: springboot-cluster\r\n          wait-for-service-stability: true<\/code><\/pre>\n<p><strong>Edit the pom.xml file and add the line<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-232\" src=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-17-194950.png\" alt=\"\" width=\"310\" height=\"34\" srcset=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-17-194950.png 310w, https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-17-194950-300x33.png 300w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/p>\n<p><em>It indicates that the name of the generated artifact will be spring-boot-ecs. This name is often used to create a JAR or WAR file, which can then be deployed to a server or container, such as Amazon Elastic Container Service (ECS), to run a Spring Boot application<\/em><\/p>\n<p><strong>Create a Dockerfile inside the project repository<\/strong><\/p>\n<pre><code>FROM maven:3.8.5-openjdk-18-slim AS build\r\n\r\nWORKDIR \/usr\/src\/app\r\n\r\nCOPY . \/usr\/src\/app\r\n\r\nRUN mvn package \r\n\r\nFROM openjdk:18-jdk-slim\r\n\r\nEXPOSE 80\r\n\r\nARG JAR_FILE=spring-boot-ecs.jar\r\n\r\nWORKDIR \/opt\/app\r\n\r\nCOPY --from=build \/usr\/src\/app\/target\/${JAR_FILE} \/opt\/app\/\r\n\r\nENTRYPOINT [\"java\",\"-jar\",\"spring-boot-ecs.jar\"]<\/code><\/pre>\n<p><strong>Then click on Action<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"my-custom-class\" src=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/action.png\" alt=\"\" width=\"957\" height=\"65\" \/><\/p>\n<p><em>Here the image is pushed into the AWS ECR repository and deployed this image into AWS ECS. After completing the deployment go to the AWS cluster, click on Task and get a public IP, copy this IP, and search into a browser.<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"my-custom-class\" src=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/app-1.png\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog describes deploying a spring-boot Application into AWS ESC via GitHub Actions. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS. Prerequisites: 1. AWS Account 2. GitHub Account Create an AWS ECR repository Login to your AWS account and create an Amazon Elastic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":24,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/217\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}