Skip to content

Commit 076eade

Browse files
committed
Updates to ec2_instance_create_delete role
1 parent 7324fd6 commit 076eade

File tree

14 files changed

+339
-469
lines changed

14 files changed

+339
-469
lines changed

roles/ec2_instance_create_delete/README.md

+82-101
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,84 @@
22

33
A role to create an EC2 instance in AWS.
44

5-
Users can specify various parameters for instance configuration, including instance type, AMI ID, key pair, tags, and VPC/subnet configuration.
6-
7-
This role also supports the creation of optional networking resources, such as an external security group and an Elastic IP (EIP). You can choose to wait for the EC2 instance to finish booting before continuing.
5+
Users can specify various parameters for instance configuration, including instance type, AMI ID, key pair, tags, VPC/subnet configuration, and whether to associate an EIP. You can choose to wait for the EC2 instance to finish booting before continuing.
6+
7+
This role can be combined with the [cloud.aws_ops.ec2_networking_resources role](../ec2_networking_resources/README.md) to create networking resources for the instance, see [examples](#examples).
8+
9+
## Requirements
10+
11+
An AWS account with the following permissions:
12+
13+
* ec2:AllocateAddress
14+
* ec2:AssociateAddress
15+
* ec2:CreateKeyPair
16+
* ec2:DeleteKeyPair
17+
* ec2:DescribeAddresses
18+
* ec2:DescribeInstanceAttribute
19+
* ec2:DescribeInstances
20+
* ec2:DescribeInstanceStatus
21+
* ec2:DescribeKeyPairs
22+
* ec2:DescribeSecurityGroups
23+
* ec2:DescribeSubnets
24+
* ec2:DescribeVpcs
25+
* ec2:DisassociateAddress
26+
* ec2:ModifyInstanceAttribute
27+
* ec2:ReleaseAddress
28+
* ec2:RunInstances
29+
* ec2:TerminateInstances
830

931
## Role Variables
1032

1133
The following variables can be set in the role to customize EC2 instance creation and networking configurations:
1234

13-
### Role Operation
14-
1535
* **ec2_instance_create_delete_operation**: (Optional)
16-
- Goal state for the instances.
17-
- "O(state=present): ensures instances exist, but does not guarantee any state (e.g. running). Newly-launched instances will be run by EC2."
18-
- "O(state=running): O(state=present) + ensures the instances are running."
19-
- "O(state=started): O(state=running) + waits for EC2 status checks to report OK if O(wait=true)."
20-
- "O(state=stopped): ensures an existing instance is stopped."
21-
- "O(state=rebooted): convenience alias for O(state=stopped) immediately followed by O(state=running)."
22-
- "O(state=restarted): convenience alias for O(state=stopped) immediately followed by O(state=started)."
23-
- "O(state=terminated): ensures an existing instance is terminated."
24-
- "O(state=absent): alias for O(state=terminated)."
25-
choices are [present, terminated, running, started, stopped, restarted, rebooted, absent]
26-
Default is `present`.
27-
28-
### EC2 Instance Configuration
29-
30-
* **ec2_instance_create_delete_aws_region**: (Required)
31-
The AWS region in which to create the EC2 instance.
36+
Target operation for the ec2 instance role. Choices are ["create", "delete"]. Defaults to "create".
3237

3338
* **ec2_instance_create_delete_instance_name**: (Required)
3439
The name of the EC2 instance to be created.
3540

36-
* **ec2_instance_create_delete_instance_type**: (Required)
37-
The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`).
41+
* **ec2_instance_create_delete_instance_type**: (Optional)
42+
The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). Required when `ec2_instance_create_delete_operation` is `true`
3843

39-
* **ec2_instance_create_delete_ami_id**: (Required)
40-
The AMI ID for the EC2 instance.
44+
* **ec2_instance_create_delete_ami_id**: (Optional)
45+
The AMI ID for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `true`
4146

4247
* **ec2_instance_create_delete_key_name**: (Optional)
4348
The name of the key pair to use for SSH access to the EC2 instance.
4449
If the key does not exist, a key pair will be created with the name.
50+
If not provided, instance will not be accessible via SSH.
4551

4652
* **ec2_instance_create_delete_vpc_subnet_id**: (Optional)
4753
The ID of the VPC subnet in which the instance will be launched.
48-
If not provided, instance might get created with `default` subnet in the AWS region if present.
54+
If not provided, instance will be created in the default subnet for the default VPC in the AWS region if present.
4955

5056
* **ec2_instance_create_delete_tags**: (Optional)
5157
A dictionary of tags to assign to the EC2 instance.
5258

53-
* **ec2_instance_create_delete_wait_for_boot**: (Optional)
54-
Whether to wait for the EC2 instance to be in the "running" or "terminated" state before continuing. Default is `true`.
55-
56-
### Optional Networking Resources
57-
58-
#### Elastic IP
59+
* **ec2_instance_create_delete_wait_for_state**: (Optional)
60+
Whether to wait for the EC2 instance to be in the "running" (if creating an instance) or "terminated" (if deleting an instance) state before continuing. Default is `true`.
5961

60-
* **ec2_instance_create_delete_vpc_id**: (Optional)
61-
The ID of the VPC used for security group and internet gateway.
62-
Required if `ec2_instance_create_delete_associate_igw` or `ec2_instance_create_delete_associate_eip` is `true`.
62+
* **ec2_instance_create_delete_associate_security_groups**: (Optional)
63+
List of security group IDs to associate with the EC2 instance.
6364

6465
* **ec2_instance_create_delete_associate_eip**: (Optional)
6566
Whether to create an Elastic IP (EIP) and associate it with the EC2 instance. Default is `false`.
66-
If set to `true` and the provided VPC doesn't have an Internet Gateway (IGW) attached, set `ec2_instance_create_delete_associate_igw` to `true` to avoid failure.
67+
If true, EC2 instance must be launched in a VPC with an Internet Gateway (IGW) attached, otherwise this will fail. Use [cloud.aws_ops.ec2_networking_resources role](../ec2_networking_resources/README.md) to create the necessary networking resources.
6768

6869
* **ec2_instance_create_delete_eip_tags**: (Optional)
6970
Tags to assign to the elastic IP.
7071

71-
#### Internet Gateway
72-
73-
* **ec2_instance_create_delete_associate_igw**: (Optional)
74-
Whether to create and associate an internet gateway with the EC2 instance. Default is `false`.
75-
If set to `true`, an internet gateway will be created or associated with the instance.
76-
77-
* **ec2_instance_create_delete_igw_tags**: (Optional)
78-
Tags to assign to the internet gateway.
79-
80-
#### External Security Group
72+
## Dependencies
8173

82-
* **ec2_instance_create_delete_associate_external_sg**: (Optional)
83-
Whether to create and associate an security group with the EC2 instance. Default is `false`.
84-
If set to `true`, an security group will be created or associated with the instance.
74+
- role: [aws_setup_credentials](../aws_setup_credentials/README.md)
8575

86-
* **ec2_instance_create_delete_external_sg_name**: (Required)
87-
The name of the security group to use for the EC2 instance.
88-
The role will check if an SG with this name exists. If not, it will create a new one.
89-
Default is `ec2_instance_create-default-external-sg`.
76+
## Examples
9077

91-
* **ec2_instance_create_delete_external_sg_description**: (Optional)
92-
A description for the security group. Default is `Security group for external access`.
93-
94-
* **ec2_instance_create_delete_external_sg_rules**: (Optional)
95-
A list of custom rules to add to the security group. Each rule is a dictionary with `proto`, `ports`, and `cidr_ip` keys. Default is to allow SSH (port 22) from `0.0.0.0/0`.
96-
97-
* **ec2_instance_create_delete_external_sg_tags**: (Optional)
98-
Tags to assign to the security group.
99-
100-
### Example:
101-
102-
Here's an example of how to use the role in a playbook.
78+
Using the role on its own in a playbook:
10379

10480
```yaml
10581
---
106-
- name: Playbook for creating EC2 instance using cloud.aws_ops.ec2_instance_create role
82+
- name: Create EC2 instance
10783
hosts: localhost
10884
gather_facts: false
10985
roles:
@@ -118,54 +94,59 @@ Here's an example of how to use the role in a playbook.
11894
ec2_instance_create_delete_tags:
11995
Component: my-test-instance
12096
Environment: Testing
121-
ec2_instance_create_delete_wait_for_boot: true
122-
ec2_instance_create_delete_vpc_id: vpc-xxxx
123-
# Optionally, enable security group creation
124-
ec2_instance_create_delete_associate_external_sg: true
125-
ec2_instance_create_delete_external_sg_name: my-custom-sg
126-
ec2_instance_create_delete_external_sg_description: Security group for my custom access
127-
ec2_instance_create_delete_external_sg_rules:
128-
- proto: tcp
129-
ports: "80"
130-
cidr_ip: "0.0.0.0/0"
131-
ec2_instance_create_delete_external_sg_tags:
132-
Component: my-custom-sg
133-
Environment: Testing
134-
# Optionally, enable Elastic IP association
135-
ec2_instance_create_delete_associate_eip: true
136-
ec2_instance_create_delete_eip_tags:
137-
Component: my-custom-eip
138-
Environment: Testing
139-
# Optionally, enable Internet Gateway association
140-
ec2_instance_create_delete_associate_igw: true
141-
ec2_instance_create_delete_igw_tags:
142-
Environment: Testing
143-
Name: "{{ resource_prefix }}-igw"
97+
ec2_instance_create_delete_wait_for_state: true
98+
```
99+
100+
Combining the role with [cloud.aws_ops.ec2_networking_resources](../ec2_networking_resources/README.md):
144101
102+
```yaml
145103
---
146-
- name: Playbook for deleting EC2 instance and other role resources using cloud.aws_ops.ec2_instance_create role
104+
- name: Create EC2 networking resources and EC2 instance
147105
hosts: localhost
148106
gather_facts: false
149107
roles:
108+
- role: cloud.aws_ops.ec2_networking_resources:
109+
vars:
110+
ec2_networking_resources_vpc_name: my-vpc
111+
ec2_networking_resources_vpc_cidr_block: 10.0.0.0/24
112+
ec2_networking_resources_subnet_cidr_block: 10.0.0.0/25
113+
ec2_networking_resources_sg_internal_name: my-internal-sg
114+
ec2_networking_resources_sg_external_name: my-external-sg
115+
ec2_networking_resources_create_igw: true
150116
- role: cloud.aws_ops.ec2_instance_create
151117
vars:
152-
ec2_instance_create_delete_operation: absent
153-
ec2_instance_create_delete_aws_region: us-west-2
118+
ec2_instance_create_delete_operation: present
119+
ec2_instance_create_delete_instance_name: my-test-instance
120+
ec2_instance_create_delete_instance_type: t2.micro
121+
ec2_instance_create_delete_ami_id: ami-066a7fbaa12345678
122+
ec2_instance_create_delete_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}"
123+
ec2_instance_create_delete_associate_security_groups:
124+
- my-internal-sg
125+
- my-external-sg
126+
ec2_instance_create_delete_associate_eip: true
127+
```
128+
129+
Deleting an EC2 instance:
130+
131+
```yaml
132+
---
133+
- name: Delete EC2 instance
134+
hosts: localhost
135+
gather_facts: false
136+
roles:
137+
- role: cloud.aws_ops.ec2_instance_create_delete
138+
vars:
139+
ec2_instance_create_delete_operation: delete
154140
ec2_instance_create_delete_instance_name: my-test-instance
155-
ec2_instance_create_delete_wait_for_boot: true
156-
ec2_instance_create_delete_associate_external_sg: true
157-
ec2_instance_create_delete_external_sg_name: my-custom-sg
158-
ec2_instance_create_delete_associate_igw: true
159-
ec2_instance_create_delete_vpc_id: vpc-xxxx
141+
ec2_instance_create_delete_wait_for_state: true
142+
```
160143
161-
License
162-
-------
144+
## License
163145
164146
GNU General Public License v3.0 or later
165147
166148
See [LICENSE](../../LICENSE) to see the full text.
167149
168-
Author Information
169-
------------------
150+
## Author Information
170151
171152
- Ansible Cloud Content Team
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
---
2-
ec2_instance_create_delete_operation: present
2+
ec2_instance_create_delete_operation: create
3+
ec2_instance_create_delete_wait_for_state: true
34
ec2_instance_create_delete_associate_eip: false
4-
ec2_instance_create_delete_associate_external_sg: false
5-
ec2_instance_create_delete_associate_igw: false
6-
ec2_instance_create_delete_external_sg_description: "Security group for external access"
7-
ec2_instance_create_delete_external_sg_name: "ec2_instance_create-default-external-sg"
8-
ec2_instance_create_delete_wait_for_boot: true
9-
ec2_instance_create_delete_external_sg_rules:
10-
- proto: tcp
11-
ports: "22"
12-
cidr_ip: "0.0.0.0/0"

roles/ec2_instance_create_delete/meta/argument_specs.yml

+16-61
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ argument_specs:
44
short_description: A role to create an EC2 instance with optional networking resources.
55
description:
66
- A role to create an EC2 instance.
7-
- Optionally can create a security group and associate an Elastic IP with the instance.
8-
- Supports custom configurations for instance settings, including instance type, AMI, key pair, tags, VPC/subnet, and networking configurations.
7+
- Can optionally attach security groups and associate an Elastic IP with the instance.
8+
- Supports custom configurations for instance settings including instance type, AMI, key pair, tags, VPC/subnet, and networking configurations.
99
options:
1010
ec2_instance_create_delete_operation:
1111
description:
@@ -14,101 +14,56 @@ argument_specs:
1414
type: str
1515
default: create
1616
choices: [create, delete]
17-
ec2_instance_create_delete_aws_region:
18-
description:
19-
- The AWS region in which to create the EC2 instance.
20-
required: true
21-
type: str
2217
ec2_instance_create_delete_instance_name:
2318
description:
2419
- The name of the EC2 instance to be created.
2520
required: true
2621
type: str
2722
ec2_instance_create_delete_instance_type:
2823
description:
29-
- The instance type for the EC2 instance.
30-
required: true
24+
- The instance type for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `true`.
25+
required: false
3126
type: str
3227
ec2_instance_create_delete_ami_id:
3328
description:
34-
- The AMI ID for the EC2 instance.
35-
required: true
29+
- The AMI ID for the EC2 instance. Required when `ec2_instance_create_delete_operation` is `true`.
30+
required: false
3631
type: str
3732
ec2_instance_create_delete_key_name:
3833
description:
39-
- The name of the key pair to use for SSH access to the EC2 instance.
34+
- The name of the key pair to use for SSH access to the EC2 instance. If the key does not exist, a key pair will be created with the name. If not provided, instance will not be accessible via SSH.
4035
required: false
4136
type: str
4237
ec2_instance_create_delete_vpc_subnet_id:
4338
description:
44-
- The ID of the VPC subnet in which the instance will be launched.
39+
- The ID of the VPC subnet in which the instance will be launched. If not provided, instance will be created in the default subnet for the default VPC in the AWS region, if present.
4540
required: false
4641
type: str
4742
ec2_instance_create_delete_tags:
4843
description:
4944
- A dictionary of tags to assign to the EC2 instance.
5045
required: false
5146
type: dict
52-
ec2_instance_create_delete_wait_for_boot:
47+
ec2_instance_create_delete_wait_for_state:
5348
description:
54-
- Whether to wait for the EC2 instance to be in the running state before continuing.
49+
- Whether to wait for the EC2 instance to be in the running/terminated state before continuing.
5550
required: false
5651
default: true
5752
type: bool
58-
ec2_instance_create_delete_associate_eip:
59-
description:
60-
- Whether to create and associate an Elastic IP (EIP) with the EC2 instance.
61-
required: false
62-
default: false
63-
type: bool
64-
ec2_instance_create_delete_associate_external_sg:
65-
description:
66-
- Whether to associate an existing or a new security group for external access.
67-
required: false
68-
default: false
69-
type: bool
70-
ec2_instance_create_delete_external_sg_name:
71-
description:
72-
- The name of the security group to create.
73-
required: false
74-
default: "ec2_instance_create-default-external-sg"
75-
type: str
76-
ec2_instance_create_delete_external_sg_description:
77-
description:
78-
- A description of the security group.
79-
required: false
80-
default: "Security group for external access"
81-
type: str
82-
ec2_instance_create_delete_external_sg_tags:
53+
ec2_instance_create_delete_associate_security_groups:
8354
description:
84-
- Tags to assign to the security group.
55+
- List of security group names or IDs to associate with the EC2 instance.
8556
required: false
86-
type: dict
87-
ec2_instance_create_delete_associate_igw:
57+
type: list
58+
elements: str
59+
ec2_instance_create_delete_associate_eip:
8860
description:
89-
- Whether to create and associate an internal gateway.
61+
- Whether to create and associate an Elastic IP (EIP) with the EC2 instance.
9062
required: false
9163
default: false
9264
type: bool
93-
ec2_instance_create_delete_vpc_id:
94-
description:
95-
- The ID of the VPC used for security group and internet gateway.
96-
- This is required when `ec2_instance_create_delete_associate_external_sg` or `ec2_instance_create_delete_associate_igw` is `true`.
97-
required: false
98-
type: str
9965
ec2_instance_create_delete_eip_tags:
10066
description:
10167
- Tags to assign to the Elastic IP.
10268
required: false
10369
type: dict
104-
ec2_instance_create_delete_external_sg_rules:
105-
description:
106-
- A list of dict containing custom rules to add to the security group. Each rule is a dictionary with `proto`, `ports`, and `cidr_ip` keys. Default is to allow SSH (port 22) from `0.0.0.0/0`.
107-
required: false
108-
type: list
109-
elements: dict
110-
ec2_instance_create_delete_igw_tags:
111-
description:
112-
- Tags to assign to the internet gateway.
113-
required: false
114-
type: dict

0 commit comments

Comments
 (0)