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-instance-with-no-vpc.md.
A documentation index is available at /llms.txt.
EC2 instances must be launched into a VPC subnet so they are subject to VPC network controls such as security groups, network ACLs, private addressing, and VPC flow logs. Without a subnet assignment, instances can lack network isolation and be exposed to the public network or miss critical network monitoring.
For Ansible EC2 modules (amazon.aws.ec2_instance, ec2_instance), the vpc_subnet_id property must be defined and set to a valid VPC subnet ID. Tasks with state equal to absent or list are ignored. Resources missing vpc_subnet_id or with it undefined are flagged.
Secure example Ansible task:
- name:Launch EC2 instance in VPC subnetamazon.aws.ec2_instance:name:my-instanceimage_id:ami-0123456789abcdef0instance_type:t3.microvpc_subnet_id:subnet-0abc1234def567890security_groups:- sg-0a1b2c3d4e5f6g7h
Compliant Code Examples
- name:Start an instance and have it begin a Tower callback on boot v3amazon.aws.ec2_instance:name:tower-callback-testkey_name:prod-ssh-keyvpc_subnet_id:subnet-5ca1ab1esecurity_group:defaulttower_callback:# IP or hostname of tower servertower_address:1.2.3.4job_template_id:876host_config_key:'[secret config key goes here]'network:assign_public_ip:trueimage_id:ami-123456cpu_credit_specification:unlimitedtags:SomeThing:A value- name:Start an instance and have it begin a Tower callback on boot v4amazon.aws.ec2:key_name:mykeyinstance_type:t2.microimage:ami-123456wait:yesgroup:webservercount:3vpc_subnet_id:subnet-29e63245assign_public_ip:yes
Non-Compliant Code Examples
- name:Start an instance and have it begin a Tower callback on bootamazon.aws.ec2_instance:name:"tower-callback-test"key_name:"prod-ssh-key"security_group:defaulttower_callback:tower_address:1.2.3.4job_template_id:876host_config_key:'[secret config key goes here]'network:assign_public_ip:trueimage_id:ami-123456cpu_credit_specification:unlimitedtags:SomeThing:"A value"
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.