|
| 1 | +# ec2_instance_create |
| 2 | + |
| 3 | +A role to create an EC2 instance in AWS. |
| 4 | + |
| 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. |
| 8 | + |
| 9 | +## Role Variables |
| 10 | + |
| 11 | +The following variables can be set in the role to customize EC2 instance creation and networking configurations: |
| 12 | + |
| 13 | +### Role Operation |
| 14 | + |
| 15 | +* **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. |
| 32 | + |
| 33 | +* **ec2_instance_create_delete_instance_name**: (Required) |
| 34 | + The name of the EC2 instance to be created. |
| 35 | + |
| 36 | +* **ec2_instance_create_delete_instance_type**: (Required) |
| 37 | + The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). |
| 38 | + |
| 39 | +* **ec2_instance_create_delete_ami_id**: (Required) |
| 40 | + The AMI ID for the EC2 instance. |
| 41 | + |
| 42 | +* **ec2_instance_create_delete_key_name**: (Optional) |
| 43 | + The name of the key pair to use for SSH access to the EC2 instance. |
| 44 | + If the key does not exist, a key pair will be created with the name. |
| 45 | + |
| 46 | +* **ec2_instance_create_delete_vpc_subnet_id**: (Optional) |
| 47 | + 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. |
| 49 | + |
| 50 | +* **ec2_instance_create_delete_tags**: (Optional) |
| 51 | + A dictionary of tags to assign to the EC2 instance. |
| 52 | + |
| 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 | + |
| 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`. |
| 63 | + |
| 64 | +* **ec2_instance_create_delete_associate_eip**: (Optional) |
| 65 | + 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 | + |
| 68 | +* **ec2_instance_create_delete_eip_tags**: (Optional) |
| 69 | + Tags to assign to the elastic IP. |
| 70 | + |
| 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 |
| 81 | + |
| 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. |
| 85 | + |
| 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`. |
| 90 | + |
| 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. |
| 103 | + |
| 104 | +```yaml |
| 105 | +--- |
| 106 | +- name: Playbook for creating EC2 instance using cloud.aws_ops.ec2_instance_create role |
| 107 | + hosts: localhost |
| 108 | + gather_facts: false |
| 109 | + roles: |
| 110 | + - role: cloud.aws_ops.ec2_instance_create |
| 111 | + vars: |
| 112 | + ec2_instance_create_delete_operation: present |
| 113 | + ec2_instance_create_delete_aws_region: us-west-2 |
| 114 | + ec2_instance_create_delete_instance_name: my-test-instance |
| 115 | + ec2_instance_create_delete_instance_type: t2.micro |
| 116 | + ec2_instance_create_delete_ami_id: ami-066a7fbaa12345678 |
| 117 | + ec2_instance_create_delete_vpc_subnet_id: subnet-071443aa123456789 |
| 118 | + ec2_instance_create_delete_tags: |
| 119 | + Component: my-test-instance |
| 120 | + 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" |
| 144 | + |
| 145 | +--- |
| 146 | +- name: Playbook for deleting EC2 instance and other role resources using cloud.aws_ops.ec2_instance_create role |
| 147 | + hosts: localhost |
| 148 | + gather_facts: false |
| 149 | + roles: |
| 150 | + - role: cloud.aws_ops.ec2_instance_create |
| 151 | + vars: |
| 152 | + ec2_instance_create_delete_operation: absent |
| 153 | + ec2_instance_create_delete_aws_region: us-west-2 |
| 154 | + 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 |
| 160 | + |
| 161 | +License |
| 162 | +------- |
| 163 | + |
| 164 | +GNU General Public License v3.0 or later |
| 165 | + |
| 166 | +See [LICENSE](../../LICENSE) to see the full text. |
| 167 | + |
| 168 | +Author Information |
| 169 | +------------------ |
| 170 | + |
| 171 | +- Ansible Cloud Content Team |
0 commit comments