cloud:aws:cloudformation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cloud:aws:cloudformation [2023/12/05 12:29] – skipidar | cloud:aws:cloudformation [2023/12/05 15:02] (current) – skipidar | ||
---|---|---|---|
Line 3: | Line 3: | ||
==== Why is Terraform better? ==== | ==== Why is Terraform better? ==== | ||
- | - " | + | * " |
- | - " | + | |
- | - " | + | |
+ | * CloudFormation has a very NOT user friendly lifecycle. Forcing to "is in ROLLBACK_COMPLETE state and can not be updated" | ||
+ | * Minor. Cloudformation " | ||
+ | * Cloudformation support of moving resources between stacks is very chatty | ||
+ | |||
+ | |||
+ | ==== Deploying with cloudformation ==== | ||
+ | |||
+ | If using nested-stacks first you need a bucket, | ||
+ | into which you will package nested stacks. | ||
+ | |||
+ | <sxh yaml> | ||
+ | AWSTemplateFormatVersion: | ||
+ | Description: | ||
+ | Resources: | ||
+ | |||
+ | MyS3SubstackBucket: | ||
+ | Type: AWS:: | ||
+ | Properties: | ||
+ | BucketName: my-alf-s3-package-bucket-2023-12-05 | ||
+ | AccessControl: | ||
+ | Tags: | ||
+ | - Key: Purpose | ||
+ | Value: CF stacks bucket | ||
+ | |||
+ | |||
+ | MyBucketPolicy: | ||
+ | Type: AWS:: | ||
+ | Properties: | ||
+ | Bucket: !Ref MyS3SubstackBucket | ||
+ | PolicyDocument: | ||
+ | Statement: | ||
+ | - Sid: AllowCloudFormationAccess | ||
+ | Effect: Allow | ||
+ | Principal: | ||
+ | Service: cloudformation.amazonaws.com | ||
+ | Action: s3:* | ||
+ | Resource: !Join | ||
+ | - '' | ||
+ | - - ' | ||
+ | - !Ref MyS3SubstackBucket | ||
+ | - /* | ||
+ | </ | ||
+ | |||
+ | now deploy the bucket | ||
+ | <sxh shell> | ||
+ | |||
+ | FILENAME=" | ||
+ | STACKNAME=" | ||
+ | REGION=" | ||
+ | |||
+ | # validate | ||
+ | aws cloudformation validate-template --template-body file:// | ||
+ | |||
+ | |||
+ | # check the change set | ||
+ | aws cloudformation deploy --stack-name $STACKNAME --template-file $FILENAME --region $REGION --no-execute-changeset | ||
+ | |||
+ | |||
+ | # execute via " | ||
+ | aws cloudformation deploy --stack-name $STACKNAME --template-file $FILENAME --region $REGION | ||
+ | |||
+ | |||
+ | # delete stack | ||
+ | # aws cloudformation delete-stack --stack-name $STACKNAME | ||
+ | </ | ||
+ | |||
+ | |||
+ | parent1.cloudformation.yaml | ||
+ | <sxh yaml> | ||
+ | AWSTemplateFormatVersion: | ||
+ | Description: | ||
+ | |||
+ | Parameters: | ||
+ | |||
+ | VpcIdParameter: | ||
+ | Type: String | ||
+ | Default: " | ||
+ | |||
+ | packageBucket: | ||
+ | Type: String | ||
+ | Default: " | ||
+ | |||
+ | Resources: | ||
+ | |||
+ | SubStack1: | ||
+ | Type: AWS:: | ||
+ | Properties: | ||
+ | TemplateURL: | ||
+ | Parameters: | ||
+ | VpcId: !Ref VpcIdParameter | ||
+ | |||
+ | </ | ||
+ | |||
+ | substack.helloworld.cloudformation.yaml | ||
+ | <sxh yaml> | ||
+ | AWSTemplateFormatVersion: | ||
+ | Description: | ||
+ | |||
+ | Parameters: | ||
+ | VpcId: | ||
+ | Type: String | ||
+ | |||
+ | |||
+ | Resources: | ||
+ | |||
+ | MySecurityGroup: | ||
+ | Type: AWS:: | ||
+ | Properties: | ||
+ | GroupDescription: | ||
+ | VpcId: !Ref VpcId | ||
+ | SecurityGroupIngress: | ||
+ | - IpProtocol: tcp | ||
+ | FromPort: 80 | ||
+ | ToPort: 80 | ||
+ | CidrIp: 0.0.0.0/0 # Example: Allowing HTTP traffic from anywhere (Please adjust for your use case) | ||
+ | Tags: | ||
+ | - Key: Name | ||
+ | Value: MySecurityGroup | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | now you can package the stack | ||
+ | |||
+ | * the sub-stacks will end up in the package-bucket. | ||
+ | * a new file `packaged-root-template.yaml` is generated, where the `TemplateURL` field is replaced by s3 references. | ||
+ | * you can deploy the parent stack and see nested stacks being deployed too. | ||
+ | |||
+ | |||
+ | |||
+ | <sxh shell> | ||
+ | set -e | ||
+ | |||
+ | FILENAME=" | ||
+ | STACKNAME=" | ||
+ | REGION=" | ||
+ | PACKAGEBUCKET=" | ||
+ | |||
+ | |||
+ | # validate | ||
+ | # aws cloudformation validate-template --template-body file:// | ||
+ | |||
+ | |||
+ | # package uploading substacks | ||
+ | rm packaged-root-template.yaml | ||
+ | aws cloudformation package --template-file $FILENAME --s3-bucket $PACKAGEBUCKET | ||
+ | |||
+ | |||
+ | |||
+ | # check the change set, dont execute : " | ||
+ | # aws cloudformation deploy --stack-name $STACKNAME --template-file $FILENAME --region $REGION --no-execute-changeset | ||
+ | |||
+ | # arn of change set is printed, here arn: | ||
+ | |||
+ | # can see change-set | ||
+ | # aws cloudformation describe-change-set --change-set-name arn: | ||
+ | |||
+ | # can continue via | ||
+ | # aws cloudformation execute-change-set --change-set-name arn: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | # execute via " | ||
+ | aws cloudformation deploy --stack-name $STACKNAME --template-file packaged-root-template.yaml --region $REGION | ||
+ | |||
+ | |||
+ | # delete stack | ||
+ | # aws cloudformation delete-stack --stack-name $STACKNAME | ||
+ | </ | ||
cloud/aws/cloudformation.1701779347.txt.gz · Last modified: by skipidar