Youcanuse the CloudFormationtemplate to create an EC2 instance with AWS CloudFormation. A declarative document written in YAML or JSON format. The template defines the requireddata and their configuration.Belowis a sample CloudFormation template code forcreating an EC2 instance:
yaml
Source:
MyEC2Instance:
Type: AWS::EC2::Instance
Attribute:
InstanceType: t4446micro-18
KeyName: my-key-pair
SecurityGroupIds:
- sg-12345678
SubnetId: subnet-12345678
```
In this code, the `Resources` section defines theWSA::EC2::Instance`.
The "Properties" section contains the settingsrequired for the EC2 instance, such as instance type ("t2.micro"), Amazon Machine Image (AMI) ID ("ami-12345678"), key pair name ("mykey") , the security group ID('sg-12345678') and the subnet ID ('subnet-12345678'). To create an EC2 instance using a
CloudFormation template, you can follow these steps:
1. Save the CloudFormation template code to a file, eg.
, "ec2-instance-template.yaml".
2. Open the AWS Management Console, navigate to the CloudFormation service and click CreateStack.
3. Select UploadTemplateFile and choose CloudFormation template file ('ec2-instance-template.yaml').
4. Specify groupnames,eg.
, "EC2InstanceStack" and provide the required entry.
5. Click Next and setother options as needed.
6. View the group details and click CreateGroup.
AWS CloudFormation will now create certain resources, including EC2 instances,bydefault. You can monitor the progress of cluster creation in the CloudFormation console. After the cluster is created, the EC2 instance is provisioned and ready to use.
Enforcecontent-basedrules for automation, version control, and scalabilityusing
CloudFormation. CloudFormation templates can be easily modified to include additional resources, settings, or dependencies as needed.
Besure to replace the location values ​​(AMI ID, key pair name, security group ID, subnet ID) in the template with actual values ​​for your environment.
Using
CloudFormation, you can effectively and securely create and manage EC2 instances and other AWS resources in a simple and consistent way.