To run AWS Launch template CLI command in CI/CD pipeline

0

Hi AWS, I am trying to create an EC2 launch template using GitHub Actions and here is the code for the same:


name: Deploying a CI/CD for Amazon EC2 AMI

on:
  workflow_dispatch:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]



jobs:

      deploy:
        runs-on: [ self-hosted, Windows, X64 ]
        timeout-minutes: 10
        defaults:
          run:
           shell: cmd
        steps:
         - name: Set AWS credentials
           uses: aws-actions/configure-aws-credentials@v1
           with:
             aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
             aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
             aws-region: ${{ secrets.AWS_REGION }}

         - name: Create the AMI for the EC2 instance
           shell: powershell
           run: |
             $instance_id = Invoke-RestMethod -Uri http://169.254.169.254/latest/meta-data/instance-id
             $ami_name="poc-ami-$(Get-Date -Format 'yyyyMMddHHmmss')"
             $ami_id=(aws ec2 create-image --instance-id $instance_id --name "$ami_name" --tag-specifications "ResourceType=image,Tags=[{Key=Name,Value=$ami_name}]" --no-reboot --output text).ImageId
             echo "AMI_ID=$ami_id" >> $GITHUB_ENV
              
         - name: Create the JSON file for Launch Template specifications
           id: create-json
           uses: jsdaniell/create-json@v1.2.2
           with:
             name: "launch-template.json"
             json: |
               {
                "NetworkInterfaces": [
                  {
                    "DeviceIndex": 0,
                    "AssociatePublicIpAddress": true,
                    "Groups": ["sg-089b265ba5151931e"],
                    "DeleteOnTermination": true
                  }
                ],
                "ImageId": ${{ env.AMI_ID }},
                "InstanceType": "t2.micro",
                "TagSpecifications": [
                  {
                    "ResourceType": "instance",
                    "Tags": [
                      {"Key": "environment", "Value": "development"},
                      {"Key": "Name", "Value": "poc-lt"}
                    ]
                  },
                  {
                    "ResourceType": "volume",
                    "Tags": [
                      {"Key": "environment", "Value": "development"},
                      {"Key": "Name", "Value": "poc-lt"}
                    ]
                  }
                ],
                "BlockDeviceMappings": [
                  {
                    "DeviceName": "/dev/sda1",
                    "Ebs": {"VolumeSize": 100}
                  }
                ]
               }
               
         - name: Create EC2 Launch Template
           shell: powershell
           run: |
             $launch_template=(aws ec2 create-launch-template --launch-template-name TemplateForAutoScaling --version-description AutoScalingVersion1 --launch-template-data file://launch-template.json --region ap-south-1)
             

but while the pipeline is triggered it is throwing an error:

ERROR:

Error parsing parameter '--launch-template-data': Invalid JSON: Expecting value: line 10 column 13 (char 188)
JSON received: {
 "NetworkInterfaces": [
   {
     "DeviceIndex": 0,
     "AssociatePublicIpAddress": true,
     "Groups": ["sg-089b265ba5151931e"],
     "DeleteOnTermination": true
   }
 ],
 "ImageId": ,
 "InstanceType": "t2.micro",
 "TagSpecifications": [
   {
     "ResourceType": "instance",
     "Tags": [
       {"Key": "environment", "Value": "development"},
       {"Key": "Name", "Value": "poc-lt"}
     ]
   },
   {
     "ResourceType": "volume",
     "Tags": [
       {"Key": "environment", "Value": "development"},
       {"Key": "Name", "Value": "poc-lt"}
     ]
   }
 ],
 "BlockDeviceMappings": [
   {
     "DeviceName": "/dev/sda1",
     "Ebs": {"VolumeSize": 100}
   }
 ]
}
Error: Process completed with exit code 1.

Can you please help me fixing this small issue? FYI the GitHub self-hosted runner is configured on an Amazon EC2 Windows machine.

Thanks

2 Answers
0

It looks like the error is with "InstanceType": "t2.micro" - have you tried doing "InstanceType": t2.micro (without quotes) instead?

profile pictureAWS
EXPERT
answered 7 months ago
profile pictureAWS
EXPERT
reviewed 7 months ago
  • No I haven't but after looking at the error it seems as if ImageId parameter is taking a null value and script fails @Brettski-AWS

  • That's not what the error message says. It's saying it is expecting a value at column thirteen. "t2.micro" is a string while t2.micro is a value. If it was a null value then the error message would be different.

0

Hi,

If you implement cfn-lint in your coding environment, it will catch up such errors even before you start to create the stack.

It will save you lots of time.

See https://github.com/aws-cloudformation/cfn-lint and https://dev.to/namuny/using-cfn-lint-to-validate-your-cloudformation-template-jpa

Best,

Didier

profile pictureAWS
EXPERT
answered 7 months ago
  • Hi, I am not creating the CloudFormation stack, I am just running these CLI commands using a CI/CD tool GitHub Actions @Didier_Durand :)

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions