Terraform - AWS Sync
# Creating a security group to allow access on port 80 and 22.data
resource "aws_security_group" "webserver-sg" {
name = "webserver-sg"
description = "Allow HTTP and SSH traffic"
vpc_id = var.vpcid
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Now the security group has been created with ports 22 and 80.
Let's add port 443 to the terraform file and apply it.
# Creating a security group to allow access on port 80 and 22.data
resource "aws_security_group" "webserver-sg" {
name = "webserver-sg"
description = "Allow HTTP and SSH traffic"
vpc_id = var.vpcid
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Once we do > terraform apply we can see the security group updated in the AWS Console.
Now, let's delete the port 443 added to the security group from the AWS Console.
Finally, we need to sync the AWS manual changes with the terraform state file.
PS C:\Users\Ex7_ALB> terraform plan --refresh-only
aws_security_group.webserver-sg: Refreshing state... [id=sg-010f820f465357ca2]
Note: Objects have changed outside of Terraform
Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have affected this plan:
Note: Objects have changed outside of Terraform
PS C:\Users\Ex7_ALB> terraform apply --refresh-only
PS C:\Users\-Ex7_ALB> terraform output
sg_id = "sg-010f820f465357ca2"
sg_name = "webserver-sg"
sg_rule = toset([
{
"cidr_blocks" = tolist([
"0.0.0.0/0",
])
"description" = ""
"from_port" = 22
"ipv6_cidr_blocks" = tolist([])
"prefix_list_ids" = tolist([])
"protocol" = "tcp"
"security_groups" = toset([])
"self" = false
"to_port" = 22
},
{
"cidr_blocks" = tolist([
"0.0.0.0/0",
])
"description" = ""
"from_port" = 443
"ipv6_cidr_blocks" = tolist([])
"prefix_list_ids" = tolist([])
"protocol" = "tcp"
"security_groups" = toset([])
"self" = false
"to_port" = 443
},
{
"cidr_blocks" = tolist([
"0.0.0.0/0",
])
"description" = ""
"from_port" = 80
"ipv6_cidr_blocks" = tolist([])
"prefix_list_ids" = tolist([])
"protocol" = "tcp"
"security_groups" = toset([])
"self" = false
"to_port" = 80
},
])
Now, the terraform state file is synced with manual configurations made.
Comments
Post a Comment