Skip to content

Commit 7054726

Browse files
GomathiselviSpatchback[bot]
authored andcommitted
Merge pull request #129 from GomathiselviS/sec_key
Add security for keypair creation in configure_ec2 pattern (cherry picked from commit c7c6737)
1 parent 6614b25 commit 7054726

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

extensions/patterns/configure_ec2/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
This pattern is designed to help get an EC2 instance up and running.
66

7+
To enable SSH access to the EC2 instance from your local machine, you need to do 2 things:
8+
9+
1. **Provide the Key Name**: Specify an existing key name in the **key_name** parameter in the survey. The EC2 instance will be associated with the key pair corresponding to the provided name. If the key pair is unavailable, you will not be able to access the instance from your local machine.
10+
11+
2. **Add a Security Group Rule for SSH Access**: Configure a security group rule to allow inbound SSH traffic from your local machine's IP address. Provide this rule in the **sg_rules** parameter in the survey. Following is an example of the security group rule:
12+
13+
```yaml
14+
- proto: tcp
15+
ports: 22
16+
cidr_ip: 203.0.113.0/3
17+
```
18+
719
## What This Pattern Covers
820
921
### Projects

extensions/patterns/configure_ec2/playbooks/create_ec2_instance.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
ansible.builtin.set_fact:
1515
final_sg_rules: "{{ create_external_access_resources | ternary(sg_rules_list + allow_external_access_sg_rules, sg_rules_list) }}"
1616

17+
- name: Validate key if given
18+
when: key_name is defined and key_name != ''
19+
block:
20+
- name: Check if the key exists
21+
amazon.aws.ec2_key_info:
22+
names:
23+
- "{{ key_name }}"
24+
register: key_info_result
25+
26+
- name: Set manage_ec2_instance_key_name role var
27+
ansible.builtin.set_fact:
28+
manage_ec2_instance_key_name: "{{ key_name }}"
29+
when: key_info_result.keypairs | length > 0
30+
1731
- name: Get RHEL 9 AMI ID if needed
1832
when: ami_id | default("", true) == ""
1933
block:
@@ -25,6 +39,7 @@
2539
owner:
2640
- amazon
2741
register: images
42+
2843
- name: Update ami_id variable
2944
ansible.builtin.set_fact:
3045
ami_id: "{{ (images.images | sort(attribute='name') | last).image_id }}"
@@ -50,9 +65,13 @@
5065
manage_ec2_instance_instance_name: "{{ instance_name }}"
5166
manage_ec2_instance_instance_type: "{{ instance_type }}"
5267
manage_ec2_instance_ami_id: "{{ ami_id }}"
53-
manage_ec2_instance_key_name: "{{ key_name }}"
5468
manage_ec2_instance_vpc_subnet_id: "{{ ec2_networking_resources_subnet_result.subnet.id }}"
5569
manage_ec2_instance_wait_for_state: "{{ wait_for_state | bool }}"
5670
manage_ec2_instance_associate_security_groups: "{{ [sg_name] }}"
5771
manage_ec2_instance_associate_eip: "{{ create_external_access_resources }}"
5872
manage_ec2_instance_instance_tags: "{{ instance_tags | default('{}', true) | from_json }}"
73+
74+
- name: Warn if key does not exist
75+
ansible.builtin.debug:
76+
msg: "Warning: The key '{{ key_name }}' does not exist!"
77+
when: key_name is defined and key_info_result.keypairs | length == 0

extensions/patterns/configure_ec2/playbooks/group_vars/all.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
key_name: "{{ instance_name }}-key"
21
wait_for_state: true
32
vpc_name: "{{ instance_name }}-vpc"
43
vpc_cidr: 10.0.0.0/24

extensions/patterns/configure_ec2/template_surveys/create_ec2_instance.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828

2929
- type: text
3030
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'
31+
question_description: Name of key pair to use for SSH access to the EC2 instance. If the key does not exist or not provided, the instance will not be accessible via SSH.
3232
variable: key_name
3333
required: false
3434

roles/manage_ec2_instance/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Users can specify various parameters for instance configuration, including insta
66

77
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).
88

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.
9+
The instance and key pair details are accessible via variables `ec2_instance_manage_create_result` and `ec2_instance_manage_key_pair_result`, respectively.
1010

1111
## Requirements
1212

roles/manage_ec2_instance/tasks/ec2_instance_create_operations.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
names:
2222
- "{{ manage_ec2_instance_key_name }}"
2323
register: key_info_result
24+
no_log: true
2425

2526
- name: Create new key pair
2627
amazon.aws.ec2_key:
2728
name: "{{ manage_ec2_instance_key_name }}"
2829
state: present
2930
when: key_info_result.keypairs | length == 0
3031
register: ec2_instance_manage_key_pair_result
32+
no_log: true
3133

3234
- name: Create EC2 instance with provided configuration
3335
amazon.aws.ec2_instance:
@@ -59,8 +61,3 @@
5961
msg:
6062
- "EC2 instance {{ ec2_instance.instance_ids[0] }} created successfully"
6163
- "Instance details: {{ ec2_instance_manage_create_result.instances[0] }}"
62-
63-
- name: Output private key if a new keypair was created
64-
when: ec2_instance_manage_key_pair_result.key is defined
65-
ansible.builtin.debug:
66-
msg: "A new key pair was created for ssh access to the instance. Please save this private key for reference: {{ ec2_instance_manage_key_pair_result.key.private_key }}"

0 commit comments

Comments
 (0)