99 INFRA_CORE_LOCALS_PATH ,
1010 INFRA_CORE_MAIN_PATH ,
1111 INFRA_CORE_VARIABLES_PATH ,
12+ INFRA_CORE_DATA_PATH ,
1213} from '@/generators/terraform/constants' ;
1314import { appendToFile , copy } from '@/helpers/file' ;
1415
@@ -18,19 +19,23 @@ import {
1819 AWS_TEMPLATE_PATH ,
1920} from '../constants' ;
2021
22+ const bastionDataContent = dedent `
23+ ### Begin Bastion Host ###
24+ data "aws_iam_policy" "ssm_managed_instance_core" {
25+ name = "AmazonSSMManagedInstanceCore"
26+ }
27+ ### End Bastion Host ###` ;
28+
2129const bastionLocalContent = dedent `
2230 ### Begin Bastion Host ###
2331 locals {
24- enable_bastion = true
32+ enable_bastion = true
33+ bastion_ssm_role_name = "\${local.env_namespace}-SSMInstanceRole"
34+ bastion_ssm_policy_arns = [data.aws_iam_policy.ssm_managed_instance_core.arn]
2535 }
2636 ### End Bastion Host ###` ;
2737
2838const bastionVariablesContent = dedent `
29- variable "bastion_image_id" {
30- description = "The AMI image ID for the bastion instance"
31- default = "ami-0801a1e12f4a9ccc0"
32- }
33-
3439 variable "bastion_instance_type" {
3540 description = "The bastion instance type"
3641 default = "t3.nano"
@@ -52,16 +57,26 @@ const bastionVariablesContent = dedent`
5257 }` ;
5358
5459const bastionModuleContent = dedent `
60+ module "bastion_ssm_role" {
61+ count = local.enable_bastion ? 1 : 0
62+ source = "../modules/iam_role"
63+
64+ role_name = local.bastion_ssm_role_name
65+ assume_role_services = ["ec2.amazonaws.com"]
66+ policy_arns = local.bastion_ssm_policy_arns
67+ create_instance_profile = true
68+ }
69+
5570 module "bastion" {
5671 count = local.enable_bastion ? 1 : 0
5772 source = "../modules/bastion"
5873
5974 subnet_ids = module.vpc.public_subnet_ids
6075 instance_security_group_ids = module.security_group.bastion_security_group_ids
6176
62- env_namespace = local.env_namespace
63- image_id = var.bastion_image_id
64- instance_type = var.bastion_instance_type
77+ env_namespace = local.env_namespace
78+ instance_type = var.bastion_instance_type
79+ iam_instance_profile = module.bastion_ssm_role[0].instance_profile_name
6580
6681 min_instance_count = var.bastion_min_instance_count
6782 max_instance_count = var.bastion_max_instance_count
@@ -79,16 +94,6 @@ const bastionSGMainContent = dedent`
7994 }
8095 }
8196
82- resource "aws_security_group_rule" "bastion_ingress_ssh_nimble" {
83- type = "ingress"
84- security_group_id = aws_security_group.bastion.id
85- from_port = 22
86- to_port = 22
87- protocol = "tcp"
88- cidr_blocks = ["\${var.nimble_office_ip}/32"]
89- description = "Nimble office"
90- }
91-
9297 resource "aws_security_group_rule" "bastion_egress_rds" {
9398 type = "egress"
9499 security_group_id = aws_security_group.bastion.id
@@ -97,6 +102,16 @@ const bastionSGMainContent = dedent`
97102 protocol = "tcp"
98103 source_security_group_id = aws_security_group.rds.id
99104 description = "From RDS to bastion"
105+ }
106+
107+ resource "aws_security_group_rule" "bastion_egress_ssm" {
108+ type = "egress"
109+ security_group_id = aws_security_group.bastion.id
110+ from_port = 443
111+ to_port = 443
112+ protocol = "tcp"
113+ cidr_blocks = ["0.0.0.0/0"]
114+ description = "Allow outbound HTTPS traffic for SSM"
100115 }` ;
101116
102117const bastionSGOutputsContent = dedent `
@@ -121,6 +136,7 @@ const applyAwsBastion = async (options: AwsOptions) => {
121136 bastionLocalContent ,
122137 options . projectName
123138 ) ;
139+ appendToFile ( INFRA_CORE_DATA_PATH , bastionDataContent , options . projectName ) ;
124140 appendToFile (
125141 INFRA_CORE_VARIABLES_PATH ,
126142 bastionVariablesContent ,
0 commit comments