aggirih021

Membuat Template AWS Cloudformation dengan AI

10 Mei 2023

 

    Ketika kita masuk ke aplikasi AI, kemudian mengetikan: "template aws cloudformation to build microservices" Maka akan keluar contoh konfigurasi AWS Cloudformation yang ditulis dalam yaml.

    AI ini sangat berguna untuk menyederhanakan langkah atau memangkas waktu DevOps engineer untuk membuat template Cloudfromation untuk membangun layanan berbasis microservices. Layanan yang sudah sangat sering & populer di dunia cloud & rekayasa perangkat lunak.

Ini adalah template yang digenerate oleh Aplikasi AI itu. Tapi apakah AWS CloudFormation? 

AWS CloudFormation is an infrastructure as code (IaC) service that allows you to easily model, provision, and manage AWS and third-party resources. 


AWS Cloudformation dalam Bahasa


 Berikut adalah contoh konfigurasi yang dihasilkan AI untuk template aws cloudformation to build microservices

---

AWSTemplateFormatVersion: "2010-09-09"

Description: CloudFormation template for building microservices


Resources:

  VPC:

    Type: AWS::EC2::VPC

    Properties:

      CidrBlock: "10.0.0.0/16"


  PublicSubnet1:

    Type: AWS::EC2::Subnet

    Properties:

      VpcId: !Ref VPC

      CidrBlock: "10.0.1.0/24"

      AvailabilityZone: us-west-2a


  PublicSubnet2:

    Type: AWS::EC2::Subnet

    Properties:

      VpcId: !Ref VPC

      CidrBlock: "10.0.2.0/24"

      AvailabilityZone: us-west-2b


  InternetGateway:

    Type: AWS::EC2::InternetGateway

    Properties:

      Tags:

        - Key: Name

          Value: InternetGateway


  VPCGatewayAttachment:

    Type: AWS::EC2::VPCGatewayAttachment

    Properties:

      VpcId: !Ref VPC

      InternetGatewayId: !Ref InternetGateway


  PublicRouteTable:

    Type: AWS::EC2::RouteTable

    Properties:

      VpcId: !Ref VPC

      Tags:

        - Key: Name

          Value: PublicRouteTable


  PublicRoute:

    Type: AWS::EC2::Route

    DependsOn: VPCGatewayAttachment

    Properties:

      RouteTableId: !Ref PublicRouteTable

      DestinationCidrBlock: "0.0.0.0/0"

      GatewayId: !Ref InternetGateway


  PublicSubnet1RouteTableAssociation:

    Type: AWS::EC2::SubnetRouteTableAssociation

    Properties:

      SubnetId: !Ref PublicSubnet1

      RouteTableId: !Ref PublicRouteTable


  PublicSubnet2RouteTableAssociation:

    Type: AWS::EC2::SubnetRouteTableAssociation

    Properties:

      SubnetId: !Ref PublicSubnet2

      RouteTableId: !Ref PublicRouteTable


  SecurityGroup:

    Type: AWS::EC2::SecurityGroup

    Properties:

      GroupDescription: "Security group for microservices"

      VpcId: !Ref VPC


  ECSService:

    Type: AWS::ECS::Service

    Properties:

      Cluster: !Ref ECSCluster

      ServiceName: Microservices

      TaskDefinition: !Ref ECSTaskDefinition

      DesiredCount: 2

      LaunchType: FARGATE

      NetworkConfiguration:

        AwsvpcConfiguration:

          AssignPublicIp: ENABLED

          Subnets:

            - !Ref PublicSubnet1

            - !Ref PublicSubnet2

          SecurityGroups:

            - !Ref SecurityGroup


  ECSExecutionRole:

    Type: AWS::IAM::Role

    Properties:

      RoleName: ECSExecutionRole

      AssumeRolePolicyDocument:

        Version: "2012-10-17"

        Statement:

          - Effect: Allow

            Principal:

              Service: ecs-tasks.amazonaws.com

            Action: sts:AssumeRole

      ManagedPolicyArns:

        - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy


  ECSTaskDefinition:

    Type: AWS::ECS::TaskDefinition

    Properties:

      Family: Microservices

      Cpu: 256

      Memory: 512

      NetworkMode: awsvpc

      ExecutionRoleArn: !Ref ECSExecutionRole

      RequiresCompatibilities:

        - FARGATE

      ContainerDefinitions:

        - Name: microservice-1

          Image: <your-image-repo>/microservice-1:latest

          Essential: true

          PortMappings:

            - ContainerPort: 80

              Protocol: tcp

        - Name: microservice-2

          Image: <your-image-repo>/microservice-2:latest

          Essential: true

          PortMappings:

            - ContainerPort: 80

              Protocol: tcp


  ECSLogGroup:

    Type: AWS::Logs::LogGroup

    Properties:

      LogGroupName: /ecs/microservices

      RetentionInDays: 7


  ECSRole:

    Type: AWS::IAM::Role

    Properties:

      RoleName: ECSRole

      AssumeRolePolicyDocument:

        Version: "2012-10-17"

        Statement:

          - Effect: Allow

            Principal:

              Service: ecs.amazonaws.com

            Action: sts:AssumeRole

      Policies:

        - PolicyName: ecs-service

          PolicyDocument:

            Version: "2012-10-17"

            Statement:

              - Effect: Allow

                Action:

                  - ecs:CreateCluster

                  - ecs:DeregisterTaskDefinition

                  - ecs:DescribeClusters

                  - ecs:DescribeContainerInstances

                  - ecs:DescribeServices

                  - ecs:DescribeTaskDefinition

                  - ecs:DescribeTasks

                  - ecs:ListClusters

                  - ecs:ListContainerInstances

                  - ecs:ListServices

                  - ecs:ListTaskDefinitions

                  - ecs:ListTasks

                  - ecs:RegisterTaskDefinition

                  - ecs:StartTask

                  - ecs:StopTask

                  - ecs:UpdateService

                Resource: "*"


  ECSCluster:

    Type: AWS::ECS::Cluster

    Properties:

      ClusterName: microservices

      CapacityProviders:

        - FARGATE

      DefaultCapacityProviderStrategy:


Agustinus Giri Hartono
#My name is Giri #Agustinus Giri Hartono. #Home is where the heart is #DevOps Enthusiast

Komentar