AWS Amazon EC2 Rule
This page shows how to write Terraform and CloudFormation for Amazon EC2 Rule and write them securely.
aws_network_acl_rule (Terraform)
The Rule in Amazon EC2 can be configured in Terraform with the resource name aws_network_acl_rule
. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_network_acl_rule" "in_accepter_public_from_requester" {
provider = aws.accepter
count = length(data.aws_subnet.requester.*.cidr_block)
network_acl_id = tolist(data.aws_network_acls.accepter_public.ids)[0]
rule_number = 1000 + count.index
egress = false
resource "aws_network_acl_rule" "public_outbound" {
count = var.create_vpc && length(local.public_subnets) > 0 ? 1 : 0
network_acl_id = aws_network_acl.public_nacl[count.index].id
protocol = "-1"
rule_action = "allow"
rule_number = 110
resource "aws_network_acl_rule" "public_ingress_icmp_from_home_network" {
# Allowing all ICMP from a trusted IP helps debugging networking issues.
network_acl_id = aws_network_acl.public.id
rule_number = 100
egress = false
protocol = "icmp"
resource "aws_network_acl_rule" "in_accepter_public_from_requester" {
provider = aws.accepter
count = length(data.aws_subnet.requester.*.cidr_block)
network_acl_id = tolist(data.aws_network_acls.accepter_public.ids)[0]
rule_number = 1000 + count.index
egress = false
Security Best Practices for aws_network_acl_rule
There is 1 setting in aws_network_acl_rule that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
Parameters
-
cidr_block
optional - string -
egress
optional - bool -
from_port
optional - number -
icmp_code
optional - string -
icmp_type
optional - string -
id
optional computed - string -
ipv6_cidr_block
optional - string -
network_acl_id
required - string -
protocol
required - string -
rule_action
required - string -
rule_number
required - number -
to_port
optional - number
Explanation in Terraform Registry
Creates an entry (a rule) in a network ACL with the specified rule number.
NOTE on Network ACLs and Network ACL Rules: Terraform currently provides both a standalone Network ACL Rule resource and a Network ACL resource with rules defined in-line. At this time you cannot use a Network ACL with in-line rules in conjunction with any Network ACL Rule resources. Doing so will cause a conflict of rule settings and will overwrite rules.
Tips: Best Practices for The Other AWS Amazon EC2 Resources
In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_ebs_volume
Ensure to use a customer-managed key for EBS volume encryption
It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).
aws_instance
Ensure to avoid storing AWS access keys in user data
It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
AWS::EC2::NetworkAcl (CloudFormation)
The NetworkAcl in EC2 can be configured in CloudFormation with the resource name AWS::EC2::NetworkAcl
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::NetworkAcl
Properties:
Tags:
- Key: Name
Value: elb1app1acl
VpcId:
Type: AWS::EC2::NetworkAcl
Properties:
Tags:
- Key: Name
Value: elbpubacl
VpcId:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref VPC
myNetworkAcl2:
Type: AWS::EC2::NetworkAcl
Properties:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref VPC
myNetworkAcl2:
Type: AWS::EC2::NetworkAcl
Properties:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref VPC
myNetworkAcl2:
Type: AWS::EC2::NetworkAcl
Properties:
"Type" : "AWS::EC2::NetworkAcl",
"Properties" : {
"VpcId" : { "Fn::ImportValue" : {"Fn::Sub" : "prod-network-ProdVpc" } },
"Tags" : [ { "Key" : "Name", "Value" : "Prod Server Subnet A Network Acl" } ]
}
},
"Type" : "AWS::EC2::NetworkAcl",
"Properties" : {
"VpcId" : { "Fn::ImportValue" : {"Fn::Sub" : "dev-staging-network-DevStagingVpc" } },
"Tags" : [ { "Key" : "Name", "Value" : "DevStaging Server Subnet A Network Acl" } ]
}
},
"Type": "AWS::EC2::NetworkAcl"
},
"acl0115e220fcc11a9ddassociation1": {
"Properties": {
"NetworkAclId": {
"Ref": "acl0115e220fcc11a9dd"
"Type":"AWS::EC2::NetworkAcl",
"Properties":{
"VpcId":{
"Ref":"vpc"
}
}
"Type": "AWS::EC2::NetworkAcl",
"Properties": {
"VpcId": {
"Fn::ImportValue": {"Fn::Sub": "${pNetworkStackName}-VPCID"}
}
}
Parameters
Explanation in CloudFormation Registry
Specifies a network ACL for your VPC.
Frequently asked questions
What is AWS Amazon EC2 Rule?
AWS Amazon EC2 Rule is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon EC2 Rule?
For Terraform, the jrpradojr/terraform-aws-vpc-peering-inter-region, vinovee/terraform-modules and raghunadhpokkalath/2020-jun-project1-externals source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the vinithacejojohn/cfn_ELB, vinithacejojohn/cfn_ELB and stelligent/cfn_nag source code examples are useful. See the CloudFormation Example section for further details.