Reputation: 89
I created new template (*.json- see ettach)
for new instance from my images (AMIs).
How can I add the instance automatically to existing security group, elastic ip and VPC ?
Thanks
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Ec2 block device mapping",
"Resources": {
"MyEC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-1ff5111",
"AvailabilityZone": "us-west-1a",
"KeyName": "Test",
"Tags": [{
"Key": "Name",
"Value": "RoiTest"
}]
}
},
Upvotes: 0
Views: 500
Reputation: 1165
The Resource Types Reference section of the CloudFormation User Guide is a good starting point to search for details such as the ones you're asking about. Specifically, you should check out the AWS:EC2:Instance and the AWS::EC2::EIPAssociation references.
To associate an EC2 instance with a VPC security group you add the SecurityGroupIds property. To create the instance inside a VPC you actually have to define its subnet (which in turn is associated with a VPC), so you add the SubnetId property. And finally to associate an elastic IP to the instance you create an EIP association resource.
This is what your template will look like:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Ec2 block device mapping",
"Resources": {
"MyEC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-1ff5111",
"AvailabilityZone": "us-west-1a",
"SubnetId": "<your existing subnet id here>",
"SecurityGroupIds": [ "<your existing security group id here>" ],
"KeyName": "Test",
"Tags": [{
"Key": "Name",
"Value": "RoiTest"
}]
}
},
"MyEIPAssociation": {
"Type": "AWS::EC2::EIPAssociation",
"Properties": {
"AllocationId": "<your existing elastic IP allocation id here>",
"InstanceId": { "Ref": "MyEC2Instance" }
}
},
... (other resources in your template)
}
}
It might be useful to point that the SecurityGroupIds property value is an array, so you can have an instance with multiple security groups.
Upvotes: 1