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

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More