The most complex Chef recipe I have written is one that provisions multiple Amazon Web Services (AWS) resources such as EC2 instances, Security Groups, and S3 buckets. This recipe uses the AWS CLI to create a VPC, subnets, and Internet Gateway, and then uses the AWS Chef cookbook to create the resources. The recipe also includes logic to detect if the resources have already been created, and to update existing resources if needed.

Example:

# Create VPC
aws_vpc ‘my_vpc’ do
cidr_block ‘10.0.0.0/16’
internet_gateway true
enable_dns_hostnames true
enable_dns_support true
instance_tenancy :default
action :create
end

# Create subnets
aws_subnet ‘my_subnet_1’ do
vpc_id ‘my_vpc’
cidr_block ‘10.0.1.0/24’
availability_zone ‘us-east-1a’
action :create
end

aws_subnet ‘my_subnet_2’ do
vpc_id ‘my_vpc’
cidr_block ‘10.0.2.0/24’
availability_zone ‘us-east-1b’
action :create
end

# Create Security Group
aws_security_group ‘my_security_group’ do
vpc_id ‘my_vpc’
inbound_rules [
{
ip_protocol: ‘tcp’,
from_port: 22,
to_port: 22,
cidr_ip: ‘0.0.0.0/0’
}
]
outbound_rules [
{
ip_protocol: ‘tcp’,
from_port: 0,
to_port: 65535,
cidr_ip: ‘0.0.0.0/0’
}
]
action :create
end

# Create EC2 instance
aws_instance ‘my_instance’ do
image_id ‘ami-xxxxxxxx’
instance_type ‘t2.micro’
security_groups ‘my_security_group’
subnet_id ‘my_subnet_1’
key_name ‘my_key’
action :create
end

# Create S3 bucket
aws_s3_bucket ‘my_bucket’ do
bucket_name ‘my_bucket’
action :create
end

Leave a Reply

Your email address will not be published. Required fields are marked *