DEV Community

Huy Dang
Huy Dang

Posted on

1

App runner with CloudFormation AWS (json, nodejs, java )

Refer to the previous article to understand the architectural model:
https://dev.to/huydanggdg/migrate-heroku-to-aws-1d73

Image description

1.Setup connect github

Create Connected accounts

Image description

Image description

2.Create file template json for Cloudformation

Example for nodejs

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AppRunnerService": {
      "Type": "AWS::AppRunner::Service",
      "Properties": {
        "ServiceName": "client",
        "SourceConfiguration": {
          "AuthenticationConfiguration": {
            "ConnectionArn": "arn:aws:apprunner:ap-northeast-1:68972488xxx:connection/app/xxxxxxxxxx"
          },
          "CodeRepository": {
            "RepositoryUrl": "https://github.com/huydanggdg/client",
            "SourceCodeVersion": {
              "Type": "BRANCH",
              "Value": "main"
            },
            "CodeConfiguration": {
              "ConfigurationSource": "API",
              "CodeConfigurationValues": {
                "Runtime": "NODEJS_14",
        "StartCommand": "npm run production",
                "BuildCommand": "npm install",               
                "Port": "8080"
              }
            }
          }
        },
        "InstanceConfiguration": {
          "Cpu": "1024",
          "Memory": "2048"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Example for java

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AppRunnerService": {
      "Type": "AWS::AppRunner::Service",
      "Properties": {
        "ServiceName": "java-main",
        "SourceConfiguration": {
          "AuthenticationConfiguration": {
            "ConnectionArn": "arn:aws:apprunner:ap-northeast-1:6897248xxxx:connection/app/xxxxxxx"
          },
          "CodeRepository": {
            "RepositoryUrl": "https://github.com/huydanggdg/java-main",
            "SourceCodeVersion": {
              "Type": "BRANCH",
              "Value": "main"
            },
            "CodeConfiguration": {
              "ConfigurationSource": "API",
              "CodeConfigurationValues": {
                "Runtime": "CORRETTO_8",
        "StartCommand": "java -Xms256m -jar target/gms_agm.jar .",
                "BuildCommand": "mvn package",               
                "Port": "3020"
              }
            }
          }
        },
        "InstanceConfiguration": {
          "Cpu": "2048",
          "Memory": "4096"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Example for Database

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "RDS PostgreSQL with Auto-Created VPC for Singer",
  "Parameters": {
    "DBPassword": {
      "Type": "String",
      "NoEcho": true,
      "Description": "Password for the PostgreSQL database"
    }
  },
  "Resources": {
    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/16",
        "EnableDnsSupport": true,
        "EnableDnsHostnames": true,
        "Tags": [{ "Key": "Name", "Value": "SingerVPC" }]
      }
    },
    "InternetGateway": {
      "Type": "AWS::EC2::InternetGateway",
      "Properties": {
        "Tags": [{ "Key": "Name", "Value": "SingerIGW" }]
      }
    },
    "VPCGatewayAttachment": {
      "Type": "AWS::EC2::VPCGatewayAttachment",
      "Properties": {
        "VpcId": { "Ref": "VPC" },
        "InternetGatewayId": { "Ref": "InternetGateway" }
      }
    },
    "SubnetGroup": {
      "Type": "AWS::RDS::DBSubnetGroup",
      "Properties": {
        "DBSubnetGroupDescription": "Subnets for Singer RDS",
        "SubnetIds": [
          { "Ref": "PublicSubnet1" },
          { "Ref": "PublicSubnet2" }
        ]
      }
    },
    "PublicSubnet1": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": { "Ref": "VPC" },
        "CidrBlock": "10.0.0.0/24",
        "AvailabilityZone": { "Fn::Select": [0, { "Fn::GetAZs": "" }] },
        "MapPublicIpOnLaunch": true,
        "Tags": [{ "Key": "Name", "Value": "SingerPublicSubnet1" }]
      }
    },
    "PublicSubnet2": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": { "Ref": "VPC" },
        "CidrBlock": "10.0.1.0/24",
        "AvailabilityZone": { "Fn::Select": [1, { "Fn::GetAZs": "" }] },
        "MapPublicIpOnLaunch": true,
        "Tags": [{ "Key": "Name", "Value": "SingerPublicSubnet2" }]
      }
    },
    "PostgreSQLInstance": {
      "Type": "AWS::RDS::DBInstance",
      "Properties": {
        "AllocatedStorage": "20",
        "DBInstanceClass": "db.t3.micro",
        "Engine": "postgres",
        "EngineVersion": "14",
        "MasterUsername": "admin",
        "MasterUserPassword": { "Ref": "DBPassword" },
        "DBName": "singer_db",
        "PubliclyAccessible": false,
        "DBSubnetGroupName": { "Ref": "SubnetGroup" }
      }
    }
  },
  "Outputs": {
    "PostgreSQLInstanceEndpoint": {
      "Description": "Endpoint for the PostgreSQL instance",
      "Value": { "Fn::GetAtt": ["PostgreSQLInstance", "Endpoint.Address"] }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

3.Run code

Image description

Image description

Image description

Image description
Default option and review => Submit

4.Check service
5.Delete Stack => AWS auto clear service

AWS Security LIVE! Stream

Stream AWS Security LIVE!

The best security feels invisible. Learn how solutions from AWS and AWS Partners make it a reality on Security LIVE!

Learn More

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay