App runner with CloudFormation AWS (json, nodejs, java )
Tags: apprunner, aws, cloudformation, iac
Published: 2024-06-04 17:16:19

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcwik0zq49nd4q3tplkb.png)

**1.Setup connect github**

![Create Connected accounts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eekfp5d06kumh06r4bjw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq39s4jauubm85hj89pu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq2xvua128locjz6m8vr.png)




**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"
        }
      }
    }
  }
}
```
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"
        }
      }
    }
  }
}
```
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"] }
    }
  }
}
```

**3.Run code**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y919wb7t6mw8xh9freug.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnca31er9i0x38d1un81.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7pby2izb6p1x16e6fnb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0w342vmzc993l9dq682.png)
Default option and review => Submit

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