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
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 whilet2.micro
is a value. If it was a null value then the error message would be different.