Tools: 36.Security Group Variable Setup Using Terraform

Tools: 36.Security Group Variable Setup Using Terraform

Source: Dev.to

Lab Information ## Lab Solutions ## Step 1: Create variables.tf ## Step 2: Create main.tf ## Step 3: To deploy this configuration ## Navigate to the Terraform directory: ## Initialize Terraform: ## Plan the configuration: ## Apply the configuration: ## Resources & Next Steps ## πŸ“¦ Full Code Repository: KodeKloud Learning Labs ## πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles ## πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions ## πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you ## Credits ## β€’ All labs are from: KodeKloud ## β€’ I sincerely appreciate your provision of these valuable resources. Then type yes when prompted to confirm the creation of the snapshot. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: # variables.tf # Define variable for Security Group name variable "KKE_sg" { description = "The name of the Security Group" type = string default = "xfusion-sg" } Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # variables.tf # Define variable for Security Group name variable "KKE_sg" { description = "The name of the Security Group" type = string default = "xfusion-sg" } COMMAND_BLOCK: # variables.tf # Define variable for Security Group name variable "KKE_sg" { description = "The name of the Security Group" type = string default = "xfusion-sg" } COMMAND_BLOCK: # main.tf # Create AWS Security Group with variable reference resource "aws_security_group" "this" { name = var.KKE_sg description = "Security group for xfusion environment" # Ingress rule: Allow SSH access ingress { description = "Allow SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Egress rule: Allow all outbound traffic egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = var.KKE_sg } } Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: # main.tf # Create AWS Security Group with variable reference resource "aws_security_group" "this" { name = var.KKE_sg description = "Security group for xfusion environment" # Ingress rule: Allow SSH access ingress { description = "Allow SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Egress rule: Allow all outbound traffic egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = var.KKE_sg } } COMMAND_BLOCK: # main.tf # Create AWS Security Group with variable reference resource "aws_security_group" "this" { name = var.KKE_sg description = "Security group for xfusion environment" # Ingress rule: Allow SSH access ingress { description = "Allow SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Egress rule: Allow all outbound traffic egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = var.KKE_sg } } CODE_BLOCK: cd /home/bob/terraform Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: cd /home/bob/terraform CODE_BLOCK: cd /home/bob/terraform CODE_BLOCK: terraform init Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: terraform init CODE_BLOCK: terraform init CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. CODE_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "5.91.0"... - Installing hashicorp/aws v5.91.0... - Installed hashicorp/aws v5.91.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. CODE_BLOCK: terraform plan Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: terraform plan CODE_BLOCK: terraform plan COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. CODE_BLOCK: terraform apply Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: terraform apply CODE_BLOCK: terraform apply COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_security_group.this: Creating... aws_security_group.this: Creation complete after 2s [id=sg-7b20df157cb40b96c] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_security_group.this: Creating... aws_security_group.this: Creation complete after 2s [id=sg-7b20df157cb40b96c] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. COMMAND_BLOCK: bob@iac-server ~/terraform via πŸ’  default ➜ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.this will be created + resource "aws_security_group" "this" { + arn = (known after apply) + description = "Security group for xfusion environment" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + from_port = 0 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "-1" + security_groups = [] + self = false + to_port = 0 # (1 unchanged attribute hidden) }, ] + id = (known after apply) + ingress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ] + description = "Allow SSH" + from_port = 22 + ipv6_cidr_blocks = [] + prefix_list_ids = [] + protocol = "tcp" + security_groups = [] + self = false + to_port = 22 }, ] + name = "xfusion-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "xfusion-sg" } + tags_all = { + "Name" = "xfusion-sg" } + vpc_id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_security_group.this: Creating... aws_security_group.this: Creation complete after 2s [id=sg-7b20df157cb40b96c] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.