Cloudform Templates: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-reference.html
SUMMARY:
- Example 1: Create an EC2 instance using CloudFormation Template.
- Example 2: Create two resources (EC2 instance and S3 bucket). Follow the same steps are mentioned in Example1 for execution.
- Example 3: Create EC2 instance and attach a Security group with EC2 instance.
- Example 4: Create an IAM Group and Add a user to that group.
- Example 5: Serverless End to End Web application deployment using Beanstalk
Prerequisite:
- Download and Install VSCode
- Add CloudFormation Linter Extension to VSCode
- Python latest version is to be installed on your system
- A default VPC for the users in the region must have been configured
Example 1: Create an EC2 instance using CloudFormation Template.
Step 1 Open a file .yaml is visual studio code.
Step 2: Add Cloudformation Linter Extension to your VSCode
Step 3: Run the following commands in the terminal
pip install cfn-lint.
pip install pydot
Step 4: Create a new file with extension .yaml or .yml (main.yml)
Step 5: Write the below sample code to create an EC2 instance using the Cloudformation template (change the ImageId and KeyName accordingly)
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-04893cdb768d0f9ee
InstanceType: t2.micro
KeyName: IrfanSydney
Tags:
- Key: Name
Value: irfan-ec2-viacloudform
Step 6: Go to Cloudformation Service.
Step 7: Click on Create Stack button.
- Select option Template is ready
- Select Upload a Template file
- Choose your file
Step 8: Click on Next
Step 9: Provide Stack name and Click on Next
Step 10: Review and Create the stack.
Step 11: Verify the Events and once All the resources (EC2 instance) get created then check if the EC2 instance got created or not.
Experiment: Delete Stack and Check the EC2 instance also get deleted.
Ans: yes it is deleted also.
Example 2: Create two resources (EC2 instance and S3 bucket). Follow the same steps are mentioned in Example1 for execution.
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-04893cdb768d0f9ee
InstanceType: t2.micro
KeyName: devops
Tags:
- Key: Name
Value: Ec2-Demo
S3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
BucketName: mumbairegionmy
in example2, NEED TO REMEMBER THAT BUCKET NAME CANNOT HAVE UPPERCASE. ALL MUST LOWERCASE.
BUT when deleting the cloudformation, S3 bucket is not deleted!!
Example 3: Create EC2 instance and attach a Security group with EC2 instance.
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0916f5ee07e7b15d6
InstanceType: t2.micro
KeyName: IrfanSydney
Tags:
- Key: Name
Value: irfan-EC2-viaCloudformation
SecurityGroupIds:
- sg-04fea68279857c128
if you have deleted the default VPC, then you will need to create a new security group cause if not the networks of the old security groups are different.
if you delete the cloudform for ec2 and security group, the ec2 will be deleted but security group will not.
Example 4: Create an IAM Group and Add a user to that group.
Resources:
iamGroup:
Type: AWS::IAM::Group
Properties:
GroupName: irfan-groupviacloudform
iamUser:
Type: AWS::IAM::User
Properties:
UserName: irfan-user-viacloudform
Path: /
Groups:
- irfan-groupviacloudform
LoginProfile:
Password: <addyourpassword>
PasswordResetRequired: false
if you delete the cloudform for user and groupuser, the user and groupuser also gets deleted!!
Example 5: Serverless End to End Web application deployment using Beanstalk via Cloudform
AWSTemplateFormatVersion: '2010-09-09'
Resources:
sampleApplication:
Type: AWS::ElasticBeanstalk::Application
Properties:
Description: AWS Elastic Beanstalk Sample Application
sampleApplicationVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Application Version
SourceBundle:
S3Bucket: !Sub "elasticbeanstalk-samples-${AWS::Region}"
S3Key: php-newsample-app.zip
sampleConfigurationTemplate:
Type: AWS::ElasticBeanstalk::ConfigurationTemplate
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Configuration Template
OptionSettings:
- Namespace: aws:autoscaling:asg
OptionName: MinSize
Value: '2'
- Namespace: aws:autoscaling:asg
OptionName: MaxSize
Value: '6'
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: LoadBalanced
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value: !Ref MyInstanceProfile
SolutionStackName: 64bit Amazon Linux 2 v3.3.11 running PHP 8.0
sampleEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName:
Ref: sampleApplication
Description: AWS ElasticBeanstalk Sample Environment
TemplateName:
Ref: sampleConfigurationTemplate
VersionLabel:
Ref: sampleApplicationVersion
MyInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Description: Beanstalk EC2 role
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier
- arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker
- arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier
MyInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref MyInstanceRole
Check the BeanStalk Env and application these should be created and you will be able to access the application on browser.
When you delete this cloudformation, the EC2 also got deleted!
No comments:
Post a Comment