For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/ansible-aws-ec2-instance-has-public-ip.md.
A documentation index is available at /llms.txt.
EC2 instances and launch templates that automatically receive a public IPv4 address are exposed directly to the internet, increasing the attack surface and the risk of unauthorized access or exploitation.
For Ansible tasks, check the following module properties:
For amazon.aws.ec2_launch_template / ec2_launch_template: network_interfaces.associate_public_ip_address
For amazon.aws.ec2_instance / ec2_instance: network.assign_public_ip
Each property must be explicitly set to false (or 'no') or omitted. The rule flags resources where the property is truthy (for example, true, yes) because there is no safe default.
Secure examples:
- name:Launch instance without public IP (ec2_instance)amazon.aws.ec2_instance:name:my-instancenetwork:assign_public_ip:false- name:Create launch template without public IPamazon.aws.ec2_launch_template:name:my-templatenetwork_interfaces:- device_index:0associate_public_ip_address:false
Compliant Code Examples
- amazon.aws.ec2:key_name:mykeyinstance_type:t2.microcount:3vpc_subnet_id:subnet-29e63245assign_public_ip:false- name:Create an ec2 launch templateamazon.aws.ec2_launch_template:name:my_templateimage_id:ami-04b762b4289fba92bkey_name:my_ssh_keyinstance_type:t2.micro- name:Create an ec2 launch templateamazon.aws.ec2_launch_template:name:"my_template"image_id:"ami-04b762b4289fba92b"key_name:my_ssh_keyinstance_type:t2.micronetwork_interfaces:- interface_type:interfaceipv6_addresses:[]mac_address: '0 e: 0 e: 36: 60: 67:cf'network_interface_id:eni - 061 dee20eba3b445aowner_id:'721066863947'source_dest_check:truestatus:" in -use"
Non-Compliant Code Examples
- name:Create an ec2 launch templateamazon.aws.ec2_launch_template:name:"my_template"image_id:"ami-04b762b4289fba92b"key_name:my_ssh_keyinstance_type:t2.micronetwork_interfaces:associate_public_ip_address:true- name:start an instance with a public IP addressamazon.aws.ec2_instance:name:"public-compute-instance"key_name:"prod-ssh-key"vpc_subnet_id:subnet-5ca1ab1einstance_type:c5.largesecurity_group:defaultnetwork:assign_public_ip:true
1
2
rulesets:- Ansible / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.