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

nacls.tf#L1
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
nacl.tf#L4
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
nacl-public.tf#L10
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"
nacls.tf#L1
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

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

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.

risk-label

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

You can check if the aws_network_acl_rule setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

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.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

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).

risk-label

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.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

cfn.yml#L303
    Type: AWS::EC2::NetworkAcl
    Properties:
       Tags:
         - Key: Name
           Value: elb1app1acl
       VpcId:
Acl.yml#L17
    Type: AWS::EC2::NetworkAcl
    Properties:
       Tags:
         - Key: Name
           Value: elbpubacl
       VpcId:
ec2_networkaclentry_reused_ports.yml#L11
    Type: AWS::EC2::NetworkAcl
    Properties:
      VpcId: !Ref VPC
  myNetworkAcl2:
    Type: AWS::EC2::NetworkAcl
    Properties:
ec2_networkaclentry_duplicate_rule_numbers.yml#L11
    Type: AWS::EC2::NetworkAcl
    Properties:
      VpcId: !Ref VPC
  myNetworkAcl2:
    Type: AWS::EC2::NetworkAcl
    Properties:
ec2_networkaclentry_egress_ingress_reused_ports.yml#L11
    Type: AWS::EC2::NetworkAcl
    Properties:
      VpcId: !Ref VPC
  myNetworkAcl2:
    Type: AWS::EC2::NetworkAcl
    Properties:
prod-network-acl.json#L7
            "Type" : "AWS::EC2::NetworkAcl",
            "Properties" : {
               "VpcId" :  { "Fn::ImportValue" : {"Fn::Sub" : "prod-network-ProdVpc" } },
               "Tags" : [ { "Key" : "Name", "Value" : "Prod Server Subnet A Network Acl" } ]
            }
         },
dev-staging-network-acl.json#L7
            "Type" : "AWS::EC2::NetworkAcl",
            "Properties" : {
               "VpcId" :  { "Fn::ImportValue" : {"Fn::Sub" : "dev-staging-network-DevStagingVpc" } },
               "Tags" : [ { "Key" : "Name", "Value" : "DevStaging Server Subnet A Network Acl" } ]
            }
         },
vpc08bf684639ded31d9network_acls.json#L68
            "Type": "AWS::EC2::NetworkAcl"
        },
        "acl0115e220fcc11a9ddassociation1": {
            "Properties": {
                "NetworkAclId": {
                    "Ref": "acl0115e220fcc11a9dd"
02createAcls.json#L44
            "Type":"AWS::EC2::NetworkAcl",
            "Properties":{
                "VpcId":{
                    "Ref":"vpc"
                }
            }
BaselineSecurity.json#L12
            "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.