|
| 1 | +# manage_ec2_instance |
| 2 | + |
| 3 | +A role to create or delete an EC2 instance in AWS. |
| 4 | + |
| 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/terminating 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/delete networking resources for the instance, see [examples](#examples). |
| 8 | + |
| 9 | +EC2 instance details and the private key (if a key pair is created) will be displayed as role output. The instance and key pair details are accessible via variables `ec2_instance_manage_create_result` and `ec2_instance_manage_key_pair_result`, respectively. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +An AWS account with the following permissions: |
| 14 | + |
| 15 | +* ec2:AllocateAddress |
| 16 | +* ec2:AssociateAddress |
| 17 | +* ec2:CreateKeyPair |
| 18 | +* ec2:DeleteKeyPair |
| 19 | +* ec2:DescribeAddresses |
| 20 | +* ec2:DescribeInstanceAttribute |
| 21 | +* ec2:DescribeInstances |
| 22 | +* ec2:DescribeInstanceStatus |
| 23 | +* ec2:DescribeKeyPairs |
| 24 | +* ec2:DescribeSecurityGroups |
| 25 | +* ec2:DescribeSubnets |
| 26 | +* ec2:DescribeVpcs |
| 27 | +* ec2:DisassociateAddress |
| 28 | +* ec2:ModifyInstanceAttribute |
| 29 | +* ec2:ReleaseAddress |
| 30 | +* ec2:RunInstances |
| 31 | +* ec2:TerminateInstances |
| 32 | + |
| 33 | +## Role Variables |
| 34 | + |
| 35 | +The following variables can be set in the role to customize EC2 instance creation and networking configurations: |
| 36 | + |
| 37 | +* **manage_ec2_instance_operation**: (Optional) |
| 38 | + Target operation for the ec2 instance role. Choices are ["create", "delete"]. Defaults to "create". |
| 39 | + |
| 40 | +* **manage_ec2_instance_instance_name**: (Required) |
| 41 | + The name of the EC2 instance to be created/deleted. |
| 42 | + |
| 43 | +* **manage_ec2_instance_instance_type**: (Optional) |
| 44 | + The instance type for the EC2 instance (e.g., `t2.micro`, `m5.large`). Required when `manage_ec2_instance_operation` is `create` |
| 45 | + |
| 46 | +* **manage_ec2_instance_ami_id**: (Optional) |
| 47 | + The AMI ID for the EC2 instance. Required when `manage_ec2_instance_operation` is `create` |
| 48 | + |
| 49 | +* **manage_ec2_instance_key_name**: (Optional) |
| 50 | + The name of the key pair to use for SSH access to the EC2 instance. |
| 51 | + If the key does not exist, a key pair will be created with the name. |
| 52 | + If not provided, instance will not be accessible via SSH. |
| 53 | + If provided when `manage_ec2_instance_operation` is `delete`, the keypair will also be deleted. |
| 54 | + |
| 55 | +* **manage_ec2_instance_vpc_subnet_id**: (Optional) |
| 56 | + The ID of the VPC subnet in which the instance will be launched. |
| 57 | + If not provided, instance will be created in the default subnet for the default VPC in the AWS region if present. |
| 58 | + |
| 59 | +* **manage_ec2_instance_tags**: (Optional) |
| 60 | + A dictionary of tags to assign to the EC2 instance. |
| 61 | + |
| 62 | +* **manage_ec2_instance_wait_for_state**: (Optional) |
| 63 | + 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`. |
| 64 | + |
| 65 | +* **manage_ec2_instance_associate_security_groups**: (Optional) |
| 66 | + List of security group IDs to associate with the EC2 instance. |
| 67 | + |
| 68 | +* **manage_ec2_instance_associate_eip**: (Optional) |
| 69 | + Whether to create an Elastic IP (EIP) and associate it with the EC2 instance. Default is `false`. |
| 70 | + 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. |
| 71 | + |
| 72 | +* **manage_ec2_instance_eip_tags**: (Optional) |
| 73 | + Tags to assign to the elastic IP. |
| 74 | + |
| 75 | +## Dependencies |
| 76 | + |
| 77 | +- role: [aws_setup_credentials](../aws_setup_credentials/README.md) |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +Using the role on its own in a playbook: |
| 82 | + |
| 83 | +```yaml |
| 84 | +--- |
| 85 | +- name: Create EC2 instance |
| 86 | + hosts: localhost |
| 87 | + gather_facts: false |
| 88 | + roles: |
| 89 | + - role: cloud.aws_ops.manage_ec2_instance |
| 90 | + vars: |
| 91 | + manage_ec2_instance_operation: create |
| 92 | + manage_ec2_instance_aws_region: us-west-2 |
| 93 | + manage_ec2_instance_instance_name: my-test-instance |
| 94 | + manage_ec2_instance_instance_type: t2.micro |
| 95 | + manage_ec2_instance_ami_id: ami-066a7fbaa12345678 |
| 96 | + manage_ec2_instance_vpc_subnet_id: subnet-071443aa123456789 |
| 97 | + manage_ec2_instance_tags: |
| 98 | + Component: my-test-instance |
| 99 | + Environment: Testing |
| 100 | + manage_ec2_instance_wait_for_state: true |
| 101 | +``` |
| 102 | +
|
| 103 | +Combining the role with [cloud.aws_ops.ec2_networking_resources](../ec2_networking_resources/README.md): |
| 104 | +
|
| 105 | +```yaml |
| 106 | +--- |
| 107 | +- name: Create EC2 networking resources and EC2 instance |
| 108 | + hosts: localhost |
| 109 | + gather_facts: false |
| 110 | + roles: |
| 111 | + - role: cloud.aws_ops.ec2_networking_resources: |
| 112 | + vars: |
| 113 | + ec2_networking_resources_vpc_name: my-vpc |
| 114 | + ec2_networking_resources_vpc_cidr_block: 10.0.0.0/24 |
| 115 | + ec2_networking_resources_subnet_cidr_block: 10.0.0.0/25 |
| 116 | + ec2_networking_resources_sg_internal_name: my-internal-sg |
| 117 | + ec2_networking_resources_sg_external_name: my-external-sg |
| 118 | + ec2_networking_resources_create_igw: true |
| 119 | + - role: cloud.aws_ops.manage_ec2_instance |
| 120 | + vars: |
| 121 | + manage_ec2_instance_operation: present |
| 122 | + manage_ec2_instance_instance_name: my-test-instance |
| 123 | + manage_ec2_instance_instance_type: t2.micro |
| 124 | + manage_ec2_instance_ami_id: ami-066a7fbaa12345678 |
| 125 | + manage_ec2_instance_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}" |
| 126 | + manage_ec2_instance_associate_security_groups: |
| 127 | + - my-internal-sg |
| 128 | + - my-external-sg |
| 129 | + manage_ec2_instance_associate_eip: true |
| 130 | +``` |
| 131 | +
|
| 132 | +Deleting an EC2 instance: |
| 133 | +
|
| 134 | +```yaml |
| 135 | +--- |
| 136 | +- name: Delete EC2 instance |
| 137 | + hosts: localhost |
| 138 | + gather_facts: false |
| 139 | + roles: |
| 140 | + - role: cloud.aws_ops.manage_ec2_instance |
| 141 | + vars: |
| 142 | + manage_ec2_instance_operation: delete |
| 143 | + manage_ec2_instance_instance_name: my-test-instance |
| 144 | + manage_ec2_instance_wait_for_state: true |
| 145 | +``` |
| 146 | +
|
| 147 | +## License |
| 148 | +
|
| 149 | +GNU General Public License v3.0 or later |
| 150 | +
|
| 151 | +See [LICENSE](../../LICENSE) to see the full text. |
| 152 | +
|
| 153 | +## Author Information |
| 154 | +
|
| 155 | +- Ansible Cloud Content Team |
0 commit comments