Skip to content

Commit 86cd3b0

Browse files
hakbaileypatchback[bot]
authored andcommitted
Merge pull request #125 from gravesm/ec2-experience
Add pattern for creating EC2 instance (cherry picked from commit 0237835)
1 parent aaab9d7 commit 86cd3b0

File tree

9 files changed

+369
-0
lines changed

9 files changed

+369
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Configure EC2 Instance Pattern
2+
3+
## Description
4+
5+
This pattern is designed to help get an EC2 instance up and running.
6+
7+
## What This Pattern Covers
8+
9+
### Projects
10+
11+
- **AWS Operations / Configure EC2 Instance Pattern Project**: Defined in `setup.yml`, this project helps organize and manage all necessary components for the Configure EC2 Instance pattern. It ensures that relevant files, roles, and configurations are logically arranged, making it easier to maintain and execute automation tasks.
12+
13+
### Job Templates
14+
15+
- **AWS Operations / Create EC2 Instance**: This job template is designed to streamline the process of creating an EC2 instance.
16+
- **AWS Operations / Terminate EC2 Instance**: This job template is designed to streamline the process of terminating (deleting) an EC2 instance.
17+
18+
### Playbooks
19+
20+
- **Create EC2 Instance Playbook**: This playbook creates an EC2 instance with optional networking configurations.
21+
- **Terminate EC2 Instance Playbook**: This playbook terminates (deletes) an existing EC2 instance and associated networking resources.
22+
23+
### Surveys
24+
25+
- **Create EC2 Instance Survey**: This survey provides an interactive way to specify parameters for creating the EC2 instance.
26+
- **Terminate EC2 Instance Survey**: This survey provides an interactive way to specify parameters for terminating the EC2 instance.
27+
28+
## Resources Created by This Pattern
29+
30+
1. **Project**
31+
- Ensures that all relevant files, roles, and configurations are logically arranged, facilitating easier maintenance and execution of automation tasks.
32+
33+
2. **Job Templates**
34+
- Outline the necessary parameters and configurations to perform network backups using the provided playbooks.
35+
- Provide surveys for specifying parameters needed to run the job templates.
36+
37+
## How to Use
38+
39+
1. **Use Seed Red Hat Pattern Job**
40+
- Ensure the custom EE is correctly built and available in your Ansible Automation Platform. Execute the "Seed Red Hat Pattern" job within the Ansible Automation Platform, and select the "AWS Operations" category to load this pattern.
41+
42+
2. **Use the Job Templates**
43+
- In the `AWS Operations / EC2 Instance Patterns` execute the required job template to create the EC2 instance. Monitor the job execution and verify that the instance has been successfully created.
44+
45+
## Contribution
46+
47+
Contributions to this project are welcome. Please fork the repository, make your changes, and submit a pull request.
48+
49+
## License
50+
51+
GNU General Public License v3.0 or later.
52+
53+
See [LICENSE](https://www.gnu.org/licenses/gpl-3.0.txt) to see the full text. This project is licensed under the MIT License. See the [LICENSE](https://github.com/redhat-cop/cloud.aws_ops/blob/main/LICENSE) file for details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
version: 3
3+
4+
dependencies:
5+
ansible_core:
6+
package_pip: ansible-core
7+
ansible_runner:
8+
package_pip: ansible-runner
9+
galaxy:
10+
collections:
11+
- name: cloud.aws_ops
12+
source: https://github.com/redhat-cop/cloud.aws_ops.git
13+
type: git
14+
version: main
15+
- name: amazon.aws
16+
source: https://github.com/ansible-collections/amazon.aws.git
17+
type: git
18+
version: main
19+
python:
20+
- boto3
21+
- botocore
22+
23+
images:
24+
base_image:
25+
name: docker.io/redhat/ubi9:latest
26+
27+
additional_build_steps:
28+
append_base: |
29+
RUN yum install -y git
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
- name: Create EC2 instance
3+
hosts: localhost
4+
gather_facts: false
5+
module_defaults:
6+
group/aws:
7+
aws_region: "{{ aws_region }}"
8+
tasks:
9+
- name: Get security group rules list from string input
10+
ansible.builtin.set_fact:
11+
sg_rules_list: "{{ sg_rules | from_yaml }}"
12+
13+
- name: Add external access rules to security group rules if needed
14+
ansible.builtin.set_fact:
15+
final_sg_rules: "{{ create_external_access_resources | ternary(sg_rules_list + allow_external_access_sg_rules, sg_rules_list) }}"
16+
17+
- name: Get RHEL 9 AMI ID if needed
18+
when: ami_id | default("", true) == ""
19+
block:
20+
- name: Get RHEL-9 images
21+
amazon.aws.ec2_ami_info:
22+
filters:
23+
architecture: x86_64
24+
name: "RHEL-9*"
25+
owner:
26+
- amazon
27+
register: images
28+
- name: Update ami_id variable
29+
ansible.builtin.set_fact:
30+
ami_id: "{{ (images.images | sort(attribute='name') | last).image_id }}"
31+
32+
- name: Create networking resources
33+
ansible.builtin.include_role:
34+
name: cloud.aws_ops.ec2_networking_resources
35+
vars:
36+
ec2_networking_resources_operation: create
37+
ec2_networking_resources_vpc_name: "{{ vpc_name }}"
38+
ec2_networking_resources_vpc_cidr_block: "{{ vpc_cidr }}"
39+
ec2_networking_resources_subnet_cidr_block: "{{ subnet_cidr }}"
40+
ec2_networking_resources_sg_name: "{{ sg_name }}"
41+
ec2_networking_resources_sg_description: "{{ sg_description }}"
42+
ec2_networking_resources_sg_rules: "{{ final_sg_rules }}"
43+
ec2_networking_resources_create_igw: "{{ create_external_access_resources }}"
44+
45+
- name: Create EC2 instance
46+
ansible.builtin.include_role:
47+
name: cloud.aws_ops.manage_ec2_instance
48+
vars:
49+
manage_ec2_instance_operation: create
50+
manage_ec2_instance_instance_name: "{{ instance_name }}"
51+
manage_ec2_instance_instance_type: "{{ instance_type }}"
52+
manage_ec2_instance_ami_id: "{{ ami_id }}"
53+
manage_ec2_instance_key_name: "{{ key_name }}"
54+
manage_ec2_instance_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}"
55+
manage_ec2_instance_wait_for_state: "{{ wait_for_state | bool }}"
56+
manage_ec2_instance_associate_security_groups: "{{ [sg_name] }}"
57+
manage_ec2_instance_associate_eip: "{{ create_external_access_resources }}"
58+
manage_ec2_instance_instance_tags: "{{ instance_tags | default('{}', true) | from_json }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
key_name: "{{ instance_name }}-key"
2+
wait_for_state: true
3+
vpc_name: "{{ instance_name }}-vpc"
4+
vpc_cidr: 10.0.0.0/24
5+
subnet_cidr: 10.0.0.0/25
6+
sg_name: "{{ instance_name }}-sg"
7+
sg_description: "Security group for EC2 instance {{ instance_name }}"
8+
sg_rules:
9+
- proto: tcp
10+
ports: 22
11+
cidr_ip: "{{ vpc_cidr }}"
12+
external_access: true
13+
create_external_access_resources: "{{ external_access | bool }}"
14+
allow_external_access_sg_rules:
15+
- proto: tcp
16+
ports: 80
17+
cidr_ip: 0.0.0.0/0
18+
- proto: tcp
19+
ports: 443
20+
cidr_ip: 0.0.0.0/0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: Terminate EC2 instance
3+
hosts: localhost
4+
gather_facts: false
5+
module_defaults:
6+
group/aws:
7+
aws_region: "{{ aws_region }}"
8+
tasks:
9+
- name: Set manage_ec2_instance role vars
10+
ansible.builtin.set_fact:
11+
manage_ec2_instance_operation: delete
12+
manage_ec2_instance_instance_name: "{{ instance_name }}"
13+
manage_ec2_instance_key_name: "{{ key_name | default(omit, true) }}"
14+
15+
- name: Delete EC2 instance
16+
ansible.builtin.include_role:
17+
name: cloud.aws_ops.manage_ec2_instance
18+
19+
- name: Delete networking resources
20+
ansible.builtin.include_role:
21+
name: cloud.aws_ops.ec2_networking_resources
22+
vars:
23+
ec2_networking_resources_operation: delete
24+
ec2_networking_resources_vpc_name: "{{ vpc_name }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
controller_labels:
3+
- name: cloud.aws_ops
4+
organization: "{{ organization | default('Default') }}"
5+
- name: configure_ec2_pattern
6+
organization: "{{ organization | default('Default') }}"
7+
- name: create_ec2_instance
8+
organization: "{{ organization | default('Default') }}"
9+
- name: terminate_ec2_instance
10+
organization: "{{ organization | default('Default') }}"
11+
12+
controller_projects:
13+
- name: AWS Operations / Configure EC2 Instance Pattern Project
14+
organization: "{{ organization | default('Default') }}"
15+
scm_branch: main
16+
scm_clean: false
17+
scm_delete_on_update: false
18+
scm_type: git
19+
scm_update_on_launch: true
20+
scm_url: https://github.com/redhat-cop/cloud.aws_ops.git
21+
22+
controller_templates:
23+
- name: AWS Operations / Create EC2 Instance
24+
description: This job template creates an EC2 instance and associated networking resources.
25+
ask_inventory_on_launch: true
26+
ask_credential_on_launch: true
27+
ask_verbosity_on_launch: true
28+
execution_environment: AWS Operations / Configure EC2 Instance Pattern Execution Environment
29+
project: AWS Operations / Configure EC2 Instance Pattern Project
30+
playbook: extensions/patterns/configure_ec2/playbooks/create_ec2_instance.yml
31+
job_type: run
32+
organization: "{{ organization | default('Default') }}"
33+
labels:
34+
- cloud.aws_ops
35+
- configure_ec2_pattern
36+
- create_ec2_instance
37+
survey_enabled: true
38+
survey_spec: "{{ lookup('file', pattern.path.replace('setup.yml', '') + 'template_surveys/create_ec2_instance.yml') | from_yaml }}"
39+
40+
- name: AWS Operations / Terminate EC2 Instance
41+
description: This job template terminates an EC2 instance and its associated networking resources.
42+
ask_inventory_on_launch: true
43+
ask_credential_on_launch: true
44+
ask_verbosity_on_launch: true
45+
execution_environment: AWS Operations / Configure EC2 Instance Pattern Execution Environment
46+
project: AWS Operations / Configure EC2 Instance Pattern Project
47+
playbook: extensions/patterns/configure_ec2/playbooks/terminate_ec2_instance.yml
48+
job_type: run
49+
organization: "{{ organization | default('Default') }}"
50+
labels:
51+
- cloud.aws_ops
52+
- configure_ec2_pattern
53+
- terminate_ec2_instance
54+
survey_enabled: true
55+
survey_spec: "{{ lookup('file', pattern.path.replace('setup.yml', '') + 'template_surveys/terminate_ec2_instance.yml') | from_yaml }}"
56+
57+
controller_execution_environments:
58+
- name: AWS Operations / Configure EC2 Instance Pattern Execution Environment
59+
description: Execution environment for the Configure EC2 Instance Pattern
60+
image: docker.io/hakbailey/aws_ops-ee:latest
61+
pull: always

extensions/patterns/configure_ec2/template_rhdh/configure_ec2.yml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
name: Create EC2 Instance Survey
3+
description: Survey to configure the EC2 instance creation pattern
4+
spec:
5+
- type: text
6+
question_name: AWS Region
7+
question_description: AWS region where resources should be created
8+
variable: aws_region
9+
required: true
10+
11+
- type: text
12+
question_name: Instance Name
13+
question_description: Name of EC2 instance to create
14+
variable: instance_name
15+
required: true
16+
17+
- type: text
18+
question_name: Instance Type
19+
question_description: Type of EC2 instance to create (e.g., t2.micro, m5.large)
20+
variable: instance_type
21+
required: true
22+
23+
- type: text
24+
question_name: AMI ID
25+
question_description: Amazon Machine Image (AMI) ID to use for the instance, if not provided will default to the RHEL 9 AMI for the provided region and instance type
26+
variable: ami_id
27+
required: false
28+
29+
- type: text
30+
question_name: Key Pair Name
31+
question_description: Name of key pair to use or create for SSH access to the EC2 instance. Defaults to '{{ instance_name }}-key'
32+
variable: key_name
33+
required: false
34+
35+
- type: multiplechoice
36+
question_name: Wait for State
37+
question_description: Whether to wait for the EC2 instance to be in the running state before continuing. Defaults to true
38+
variable: wait_for_state
39+
required: false
40+
choices:
41+
- "true"
42+
- "false"
43+
44+
- type: text
45+
question_name: Instance Tags
46+
question_description: 'A dict of tags for the instance, e.g. {"environment: test", "owner": "team foobar"}'
47+
variable: instance_tags
48+
required: false
49+
50+
- type: text
51+
question_name: VPC Name
52+
question_description: Name of the VPC to create. Defaults to '{{instance_name}}-vpc'
53+
variable: vpc_name
54+
required: false
55+
56+
- type: text
57+
question_name: VPC CIDR Block
58+
question_description: CIDR block to use for the VPC being created. Defaults to 10.0.0.0/24
59+
variable: vpc_cidr
60+
required: false
61+
62+
- type: text
63+
question_name: Subnet CIDR block
64+
question_description: CIDR block to use for the subnet being created. 10.0.0.0/25
65+
variable: subnet_cidr
66+
required: false
67+
68+
- type: text
69+
question_name: Security Group Name
70+
question_description: Name of the security group to create for securing traffic to the instance. Defaults to '{{ instance_name }}-sg'
71+
variable: sg_name
72+
required: false
73+
74+
- type: text
75+
question_name: Security Group Description
76+
question_description: Description for the security group. Defaults to 'Security group for EC2 instance {{ instance_name }}'
77+
variable: sg_description
78+
required: false
79+
80+
- type: textarea
81+
question_name: Security Group Rules
82+
question_description: "A list of security group rules in yaml format, e.g.:
83+
- proto: tcp
84+
ports: 80
85+
cidr_ip: 0.0.0.0/0
86+
Defaults to allowing SSH access from within the VPC"
87+
variable: sg_rules
88+
required: false
89+
90+
- type: multiplechoice
91+
question_name: Create External Access Resources
92+
question_description: Whether to create resources for external access to the EC2 instance. Defaults to true. When true, adds security groups rules allowing inbound HTTP and HTTPS traffic, creates an internet gateway, creates a custom route table routing all internet traffic to the gateway, and allocates an elastic IP address for the instance.
93+
variable: external_access
94+
required: false
95+
choices:
96+
- "true"
97+
- "false"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: Terminate EC2 Instance Survey
3+
description: Survey to configure the EC2 instance termination pattern
4+
spec:
5+
- type: text
6+
question_name: AWS Region
7+
question_description: Name of AWS region to create instance in
8+
variable: aws_region
9+
required: true
10+
11+
- type: text
12+
question_name: Instance Name
13+
question_description: Name of EC2 instance
14+
variable: instance_name
15+
required: true
16+
17+
- type: text
18+
question_name: Key Pair Name
19+
question_description: Name of key pair for instance, include to delete key pair created with other instance resources. Defaults to '{{ instance_name }}-key'
20+
variable: key_name
21+
required: false
22+
23+
- type: text
24+
question_name: VPC Name
25+
question_description: Name of the VPC to delete, include to delete VPC and associated networking resources created for instance. Defaults to '{{instance_name}}-vpc'
26+
variable: vpc_name
27+
required: false

0 commit comments

Comments
 (0)