Skip to content

Commit afd8611

Browse files
authoredOct 26, 2023
Initial commit (#50)
1 parent 6f23ef1 commit afd8611

File tree

6 files changed

+18
-15
lines changed

6 files changed

+18
-15
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ The following inputs can be used as `step.with` keys
143143
| `aws_ec2_ami_owner` | String | 'Owner of AWS AMI image. This ensures the provider is the one we are looking for. Defaults to `099720109477`, Canonical (Ubuntu).' |
144144
| `aws_ec2_ami_id` | String | AWS AMI ID. Will default to latest Ubuntu 22.04 server image (HVM). Accepts `ami-###` values. |
145145
| `aws_ec2_ami_update` | Boolean | Set this to `true` if you want to recreate the EC2 instance if there is a newer version of the AMI. Defaults to `false`.|
146-
| `aws_ec2_iam_instance_profile` | String | The AWS IAM instance profile to use for the EC2 instance. Default is `${GITHUB_ORG_NAME}-${GITHUB_REPO_NAME}-${GITHUB_BRANCH_NAME}`|
147146
| `aws_ec2_instance_type` | String | The AWS IAM instance type to use. Default is `t2.small`. See [this list](https://aws.amazon.com/ec2/instance-types/) for reference. |
148147
| `aws_ec2_instance_root_vol_size` | Integer | Define the volume size (in GiB) for the root volume on the AWS Instance. Defaults to `8`. |
149148
| `aws_ec2_instance_root_vol_preserve` | Boolean | Set this to true to avoid deletion of root volume on termination. Defaults to `false`. |
150149
| `aws_ec2_security_group_name` | String | The name of the EC2 security group. Defaults to `SG for ${aws_resource_identifier} - EC2`. |
150+
| `aws_ec2_iam_instance_profile` | String | The AWS IAM instance profile to use for the EC2 instance. Will create one if none provided with the name`${GITHUB_ORG_NAME}-${GITHUB_REPO_NAME}-${GITHUB_BRANCH_NAME}`. |
151151
| `aws_ec2_create_keypair_sm` | Boolean | Generates and manage a secret manager entry that contains the public and private keys created for the ec2 instance. |
152152
| `aws_ec2_instance_public_ip` | Boolean | Add a public IP to the instance or not. (Not an Elastic IP). |
153153
| `aws_ec2_port_list` | String | Comma separated list of ports to be enabled in the EC2 instance security group. (NOT THE ELB) In a `xx,yy` format. |

‎operations/_scripts/generate/generate_vars_terraform.sh

+1-9
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ aws_resource_identifier="aws_resource_identifier = \"${GITHUB_IDENTIFIER}\""
4444
aws_resource_identifier_supershort="aws_resource_identifier_supershort = \"${GITHUB_IDENTIFIER_SS}\""
4545

4646
# Special cases - Values that need fallback values or special calculation
47-
48-
aws_ec2_iam_instance_profile=
49-
if [ -n "${AWS_EC2_IAM_INSTANCE_PROFILE}" ]; then
50-
aws_ec2_iam_instance_profile="aws_ec2_iam_instance_profile =\"${AWS_EC2_IAM_INSTANCE_PROFILE}\""
51-
else
52-
aws_ec2_iam_instance_profile="aws_ec2_iam_instance_profile =\"${GITHUB_IDENTIFIER}\""
53-
fi
54-
5547
aws_r53_sub_domain_name=
5648
if [ -n "${AWS_R53_SUB_DOMAIN_NAME}" ]; then
5749
aws_r53_sub_domain_name="aws_r53_sub_domain_name = \"${AWS_R53_SUB_DOMAIN_NAME}\""
@@ -94,7 +86,7 @@ if [[ $(alpha_only "$AWS_EC2_INSTANCE_CREATE") == true ]]; then
9486
aws_ec2_ami_owner=$(generate_var aws_ec2_ami_owner $AWS_EC2_AMI_OWNER)
9587
aws_ec2_ami_id=$(generate_var aws_ec2_ami_id $AWS_EC2_AMI_ID)
9688
aws_ec2_ami_update=$(generate_var aws_ec2_ami_update $AWS_EC2_AMI_UPDATE)
97-
# aws_ec2_iam_instance_profile=$(generate_var aws_ec2_iam_instance_profile AWS_EC2_IAM_INSTANCE_PROFILE - Special case
89+
aws_ec2_iam_instance_profile=$(generate_var aws_ec2_iam_instance_profile $AWS_EC2_IAM_INSTANCE_PROFILE)
9890
aws_ec2_instance_type=$(generate_var aws_ec2_instance_type $AWS_EC2_INSTANCE_TYPE)
9991
aws_ec2_instance_root_vol_size=$(generate_var aws_ec2_instance_root_vol_size $AWS_EC2_INSTANCE_ROOT_VOL_SIZE)
10092
aws_ec2_instance_root_vol_preserve=$(generate_var aws_ec2_instance_root_vol_preserve $AWS_EC2_INSTANCE_ROOT_VOL_PRESERVE)

‎operations/deployment/terraform/aws/bitovi_main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module "ec2" {
1313
aws_ec2_instance_root_vol_preserve = var.aws_ec2_instance_root_vol_preserve
1414
aws_ec2_create_keypair_sm = var.aws_ec2_create_keypair_sm
1515
aws_ec2_security_group_name = var.aws_ec2_security_group_name
16+
aws_ec2_iam_instance_profile = var.aws_ec2_iam_instance_profile
1617
aws_ec2_port_list = var.aws_ec2_port_list
1718
# Data inputs
1819
aws_ec2_selected_vpc_id = module.vpc.aws_selected_vpc_id

‎operations/deployment/terraform/modules/aws/ec2/aws_ec2.tf

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
resource "aws_iam_instance_profile" "ec2_profile" {
2+
count = var.aws_ec2_iam_instance_profile != "" ? 0 : 1
23
name = var.aws_resource_identifier
3-
role = aws_iam_role.ec2_role.name
4+
role = aws_iam_role.ec2_role[0].name
5+
}
6+
7+
data "aws_iam_instance_profile" "ec2_profile_provided" {
8+
count = var.aws_ec2_iam_instance_profile != "" ? 1 : 0
9+
name = var.aws_ec2_iam_instance_profile
410
}
511

612
data "aws_ami" "image_selected" {
@@ -22,7 +28,7 @@ resource "aws_instance" "server" {
2228
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
2329
key_name = aws_key_pair.aws_key.key_name
2430
monitoring = true
25-
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
31+
iam_instance_profile = var.aws_ec2_iam_instance_profile != "" ? data.aws_iam_instance_profile.ec2_profile_provided[0].name : aws_iam_instance_profile.ec2_profile[0].name
2632
user_data_base64 = base64encode(try(file("./aws_ec2_incoming_user_data_script.sh"), ""))
2733
user_data_replace_on_change = var.aws_ec2_user_data_replace_on_change
2834
root_block_device {
@@ -51,7 +57,7 @@ resource "aws_instance" "server_ignore_ami" {
5157
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
5258
key_name = aws_key_pair.aws_key.key_name
5359
monitoring = true
54-
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
60+
iam_instance_profile = var.aws_ec2_iam_instance_profile != "" ? data.aws_iam_instance_profile.ec2_profile_provided[0].name : aws_iam_instance_profile.ec2_profile[0].name
5561
user_data_base64 = base64encode(try(file("./aws_ec2_incoming_user_data_script.sh"), ""))
5662
user_data_replace_on_change = var.aws_ec2_user_data_replace_on_change
5763
root_block_device {

‎operations/deployment/terraform/modules/aws/ec2/aws_ec2_security.tf

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ output "aws_security_group_ec2_sg_id" {
5050
}
5151

5252
resource "aws_iam_role" "ec2_role" {
53+
count = var.aws_ec2_iam_instance_profile != "" ? 0 : 1
5354
name = var.aws_resource_identifier
5455
assume_role_policy = jsonencode({
5556
Version = "2012-10-17"
@@ -68,6 +69,7 @@ resource "aws_iam_role" "ec2_role" {
6869

6970
# attach a policy to allow cloudwatch access
7071
resource "aws_iam_policy" "cloudwatch" {
72+
count = var.aws_ec2_iam_instance_profile != "" ? 0 : 1
7173
name = var.aws_resource_identifier
7274

7375
policy = <<EOF
@@ -92,6 +94,7 @@ EOF
9294
}
9395

9496
resource "aws_iam_role_policy_attachment" "cloudwatch_attach" {
95-
role = aws_iam_role.ec2_role.name
96-
policy_arn = aws_iam_policy.cloudwatch.arn
97+
count = var.aws_ec2_iam_instance_profile != "" ? 0 : 1
98+
role = aws_iam_role.ec2_role[0].name
99+
policy_arn = aws_iam_policy.cloudwatch[0].arn
97100
}

‎operations/deployment/terraform/modules/aws/ec2/aws_ec2_vars.tf

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ variable "aws_ec2_instance_root_vol_size" {}
1010
variable "aws_ec2_instance_root_vol_preserve" {}
1111
variable "aws_ec2_create_keypair_sm" {}
1212
variable "aws_ec2_security_group_name" {}
13+
variable "aws_ec2_iam_instance_profile" {}
1314
variable "aws_ec2_port_list" {}
1415
# Data inputs
1516
variable "aws_ec2_selected_vpc_id" {}

0 commit comments

Comments
 (0)
Please sign in to comment.