1

This cloud formation template works as per my expectations. But when I enable Private IP address parameter, I get an error.

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole": "arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     #"PrivateIpAddress": "18.208.241.12"
                  }
               ]
            }
         }
      }
   }
}

How do I assign a specific elastic IP address to the newly created spot instance?

If the answer is through UserData then how to mention user-data string in this template? (I have a few other commands in user-data file)


Update:

I tried to set the IP address using user-data. Spot instance was created using this script, but without any Public IP address.

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole": "arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     #"PrivateIpAddress": "18.208.241.12"
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -xe\n",
"pip install aws-ec2-assign-elastic-ip\n",
"aws-ec2-assign-elastic-ip --access-key xxx --secret-key xxx --valid-ips 18.208.241.12\n"
        ]]}}
                  }
               ]
            }
         }
      }
   }
}
1

I managed to add user-data parameter and the final code looks like this:

{
   "Resources":{
      "MySpotFleet":{
         "Type":"AWS::EC2::SpotFleet",
         "Properties":{
            "SpotFleetRequestConfigData":{
               "IamFleetRole":"arn:aws:iam::513469704633:role/aws-ec2-spot-fleet-tagging-role",
               "SpotPrice":"1",
               "TargetCapacity":"1",
               "LaunchSpecifications":[
                  {
                     "InstanceType":"t2.small",
                     "ImageId":"ami-009d6802948d06e52",
                     "KeyName":"dec15a",
                     "BlockDeviceMappings":[
                        {
                           "DeviceName":"/dev/xvda",
                           "Ebs":{
                              "VolumeSize":100,
                              "VolumeType":"gp2"
                           }
                        }
                     ],

                   "UserData": {
 "Fn::Base64": {
 "Fn::Sub": 
          "#!/bin/bash -xe\n
           yum install -y docker mysql git python-pip\n  
           pip install aws-ec2-assign-elastic-ip\n 
           sudo amazon-linux-extras install -y docker\n
           aws-ec2-assign-elastic-ip --access-key xxx --secret-key xxx --valid-ips 18.208.241.12\n
           service docker start\n"
                    }
 },

                  }
               ]
            }
         }
      }
   }
}
4
  • Nice, however never ever embed access and secret keys to your templates. Use EC2 Instance IAM Role that gives it the required permissions instead (yes it can be done through CloudFormation).
    – MLu
    Dec 4 '18 at 11:07
  • Also you seem to be doing some docker stuff - consider AWS ECS instead of running docker directly. It will make your life easier.
    – MLu
    Dec 4 '18 at 11:09
  • How do I create a role "aws-ec2-spot-fleet-tagging-role" using cloudformation template?
    – shantanuo
    Dec 5 '18 at 6:06
  • you’ll need AWS::IAM::Role resource in the template. If you need more details open a new question and I will answer there.
    – MLu
    Dec 5 '18 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.