{"id":18947648,"url":"https://github.com/yousafkhamza/cloudformation-stack-deployment-through-terraform","last_synced_at":"2025-04-15T23:30:23.958Z","repository":{"id":195965533,"uuid":"436116411","full_name":"yousafkhamza/CloudFormation-stack-deployment-through-Terraform","owner":"yousafkhamza","description":"This is a combined infrastructure deployment using AWS CloudFormation and Terraform. Also, you guys have a doubt why used both at the same time. Because I just tried to deploy a CloudFormation stack through terraform. Hence, we can avoid automating AWS manual CloudFormation selection and related steps as well, and also, we can simply use the same in a CI/CD pipeline that isn't AWS CodePipeline. Furthermore, you can use that CloudFormation code directly through the AWS console so you can skip terraform that if you don't need it. In addition, I had provided both console and terraform steps in README so please read the same and do with good practice.","archived":false,"fork":false,"pushed_at":"2021-12-11T02:25:34.000Z","size":74,"stargazers_count":2,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2023-09-20T19:52:38.647Z","etag":null,"topics":["cloudformation","hcl","terraform","yaml"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yousafkhamza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-12-08T04:31:14.000Z","updated_at":"2023-09-20T19:52:42.352Z","dependencies_parsed_at":"2023-09-20T20:13:32.240Z","dependency_job_id":null,"html_url":"https://github.com/yousafkhamza/CloudFormation-stack-deployment-through-Terraform","commit_stats":null,"previous_names":["yousafkhamza/cloudformation-stack-deployment-through-terraform"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2FCloudFormation-stack-deployment-through-Terraform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2FCloudFormation-stack-deployment-through-Terraform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2FCloudFormation-stack-deployment-through-Terraform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2FCloudFormation-stack-deployment-through-Terraform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yousafkhamza","download_url":"https://codeload.github.com/yousafkhamza/CloudFormation-stack-deployment-through-Terraform/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223688710,"owners_count":17186298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudformation","hcl","terraform","yaml"],"created_at":"2024-11-08T13:10:49.434Z","updated_at":"2024-11-08T13:10:52.376Z","avatar_url":"https://github.com/yousafkhamza.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CloudFormation-stack-deployment-through-Terraform\n\n[![Build](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger)\n\n---\n## Description\nI just try to write a CloudFormation Stack with EC2 instance \u0026 Security Group. So, this is a combined infrastructure deployment using AWS CloudFormation with Terraform. Also, you guys have a doubt why used both at the same time. Because I just tried to deploy a CloudFormation stack through Terraform. Hence, we can avoid AWS manual CloudFormation selection and related steps as well, and also, we can simply use the same in a CI/CD pipeline and easy to add or change things through Terraform. \n\nFurthermore, you can use that CloudFormation code directly through the AWS console so, then you can skip Terraform that if you don't need it. In addition, I had provided both console and terraform steps in README so please read the same and do with good practice.\n\n#### Brief\n----\nAWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample:   Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon   EC2 instance. You will be billed for the AWS resources used if you create a stack from this template. Also, I repeat the template deployment using both manual and terraform automated.\n\n----\n## Feature\n- Easy to deploy ec2 instance and security group using CF template\n- Easy to deploy CF stack through terraform \n- Region-wise AMI fetching automatically\n\n----\n## Pre-Requests\n- Existing an instance keypair\n- Basic Knowledge of Terraform\n- Basic Knowledge of CloudFormation (YAML)\n- [AWS Access Key and Secret Key](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html)\n\n#### Installations\n##### Terraform\n----\n- Linux: (Please note that only for Linux no need to download any repo or files)\n```\ncurl -Ls https://raw.githubusercontent.com/yousafkhamza/Terraform_installation/main/terraform.sh | bash\n```\n\n- [Installation Guide for others](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started)\n\n##### Git\n------\n- Linux\n```\nyum install -y git\n```\n- [Installation Guide for others](https://github.com/git-guides/install-git)\n\n-----\n## How to Get\n```\ngit clone https://github.com/yousafkhamza/CloudFormation-stack-deployment-through-Terraform\n```\n----\n## How to use with Terraform\n\u003e **Please make sure you have entered the Access key, Secret key (or either IAM role attached), and Instance keypair name so please verify terrform.tfvars and provider.tf file before as well**\n\n```\ncd CloudFormation-stack-deployment-through-Terraform\n```\n_terraform.tfvars_\n```\naws_region = \"us-east-1\"    # mention which region would you need.\nkey_name = \"owntest\"        # this one is for study purposes so you guys mention here the existing key pair name under your AWS account which you using.\ninstance_type = \"t2.micro\"  # mention which instance type would you need.\ncf_file = \"./ec2stack.yml\"  # CF stack file. so, if you have any ec2 instance and security group-related changes? then please go and change the same as CF format because that file doesn't have any dependencies with terraform.\n```\n_provider.tf_   [`if you're using IAM role you can skip the credential passing`]\n```\nprovider \"aws\" {\nregion = var.aws_region\naccess_key = your access_key\nsecret_key = your secret_key\n}\n```\n### Terraform Execution\n```\nterraform init\nterraform plan\nterraform apply\n```\n\n----\n## Output be like\n```\n[ec2-user@ip-172-31-11-179 CloudFormation-stack-deployment-through-Terraform]$ terraform apply -auto-approve\n\nTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following\nsymbols:\n  + create\n\nTerraform will perform the following actions:\n\n  # aws_cloudformation_stack.ec2_instance will be created\n  + resource \"aws_cloudformation_stack\" \"ec2_instance\" {\n      + id            = (known after apply)\n      + name          = \"ec2-instance-stack\"\n      + outputs       = (known after apply)\n      + parameters    = {\n          + \"InstanceType\" = \"t2.micro\"\n          + \"KeyName\"      = \"owntest\"\n        }\n      + policy_body   = (known after apply)\n      + tags          = {\n          + \"Name\" = \"CF-EC2-Stack\"\n        }\n      + tags_all      = {\n          + \"Name\" = \"CF-EC2-Stack\"\n        }\n      + template_body = \u003c\u003c-EOT\n            # Date: 08/12/2021\n            # Author: Yousaf K Hamza\n            # Description: CloudForamtion Stack creation with EC2 instance and Security Group in a YAML Format\n\n            # ----------------------------------------------------\n            # CloudFormation Stack in YAML\n            # ----------------------------------------------------\n            AWSTemplateFormatVersion: '2010-09-09'\n            Description: 'AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample:\n              Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based\n              on the region in which the stack is run. This example creates an EC2 security group\n              for the instance to give you SSH access. **WARNING** This template creates an Amazon\n              EC2 instance. You will be billed for the AWS resources used if you create a stack\n              from this template.'\n\n            # ----------------------------------------------------\n            # Parameters like as we can pass values like variables\n            # ----------------------------------------------------\n            Parameters:\n              KeyName:\n                Description: Name of an existing EC2 KeyPair to enable SSH access to the instance\n                Type: AWS::EC2::KeyPair::KeyName\n                ConstraintDescription: must be the name of an existing EC2 KeyPair.\n              InstanceType:\n                Description: WebServer EC2 instance type\n                Type: String\n                ConstraintDescription: must be a valid EC2 instance type.\n              SSHLocation:\n                Description: The IP address range that can be used to SSH to the EC2 instances\n                Type: String\n                MinLength: 9\n                MaxLength: 18\n                Default: 0.0.0.0/0\n                AllowedPattern: (\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})\n                ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.\n              LatestAmiId:\n                Type:  'AWS::SSM::Parameter::Value\u003cAWS::EC2::Image::Id\u003e'\n                Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'\n\n            # ----------------------------------------------------\n            # Resource Creation for EC2\n            # ----------------------------------------------------\n            Resources:\n              EC2Instance:\n                Type: AWS::EC2::Instance\n                Properties:\n                  InstanceType: !Ref 'InstanceType'\n                  SecurityGroups: [!Ref 'InstanceSecurityGroup']\n                  KeyName: !Ref 'KeyName'\n                  ImageId: !Ref 'LatestAmiId'\n                  Tags:\n                    - Key: Name\n                      Value: CF-EC2\n\n            # ----------------------------------------------------\n            # Resource Creation for Security group\n            # ----------------------------------------------------\n              InstanceSecurityGroup:\n                Type: AWS::EC2::SecurityGroup\n                Properties:\n                  GroupDescription: Enable SSH access via port 22\n                  SecurityGroupIngress:\n                  - IpProtocol: tcp\n                    FromPort: 22\n                    ToPort: 22\n                    CidrIp: !Ref 'SSHLocation'\n\n            # ----------------------------------------------------\n            # After stack creation outputs act like a terraform output\n            # ----------------------------------------------------\n            Outputs:\n              InstanceId:\n                Description: InstanceId of the newly created EC2 instance\n                Value: !Ref 'EC2Instance'\n              AZ:\n                Description: Availability Zone of the newly created EC2 instance\n                Value: !GetAtt [EC2Instance, AvailabilityZone]\n              PublicDNS:\n                Description: Public DNSName of the newly created EC2 instance\n                Value: !GetAtt [EC2Instance, PublicDnsName]\n              PublicIP:\n                Description: Public IP address of the newly created EC2 instance\n                Value: !GetAtt [EC2Instance, PublicIp]\n        EOT\n    }\n\nPlan: 1 to add, 0 to change, 0 to destroy.\naws_cloudformation_stack.ec2_instance: Creating...\naws_cloudformation_stack.ec2_instance: Still creating... [10s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [20s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [30s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [40s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [50s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [1m0s elapsed]\naws_cloudformation_stack.ec2_instance: Still creating... [1m10s elapsed]\naws_cloudformation_stack.ec2_instance: Creation complete after 1m16s [id=arn:aws:cloudformation:us-east-1:361738388880:stack/ec2-instance-stack/0803bf10-58de-11ec-88bf-12fb6e4d7bd1]\n\nApply complete! Resources: 1 added, 0 changed, 0 destroyed.\n```\n\n----\n## How to use CloudFormation stack without Terraform\n_Screenshot 1_\n![alt_txt](https://i.ibb.co/hKg0TVD/1.png)\n\n_Screenshot 2_\n![alt_txt](https://i.ibb.co/2dYzgTh/2.png)\n\n_Screenshot 3_\n![alt_txt](https://i.ibb.co/VSWmH3h/3.png)\n\n_Screenshot 4_\n\n![alt_txt](https://i.ibb.co/T4dN4YG/4.png)\n\n_Screenshot 5_\n![alt_txt](https://i.ibb.co/Pmk7Ft5/5.png)\n\n----\n## Behind the code\n_ec2stack.yml_\n```\n# ----------------------------------------------------\n# CloudFormation Stack in YAML\n# ----------------------------------------------------\nAWSTemplateFormatVersion: '2010-09-09'\nDescription: 'AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample:\n  Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based\n  on the region in which the stack is run. This example creates an EC2 security group\n  for the instance to give you SSH access. **WARNING** This template creates an Amazon\n  EC2 instance. You will be billed for the AWS resources used if you create a stack\n  from this template.'\n\n# ----------------------------------------------------\n# Parameters like as we can pass values like variables\n# ----------------------------------------------------\nParameters:\n  KeyName:\n    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance\n    Type: AWS::EC2::KeyPair::KeyName\n    ConstraintDescription: must be the name of an existing EC2 KeyPair.\n  InstanceType:\n    Description: WebServer EC2 instance type\n    Type: String\n    ConstraintDescription: must be a valid EC2 instance type.\n  SSHLocation:\n    Description: The IP address range that can be used to SSH to the EC2 instances\n    Type: String\n    MinLength: 9\n    MaxLength: 18\n    Default: 0.0.0.0/0\n    AllowedPattern: (\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})\n    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.\n  LatestAmiId:\n    Type:  'AWS::SSM::Parameter::Value\u003cAWS::EC2::Image::Id\u003e'\n    Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'\n\n# ----------------------------------------------------\n# Resource Creation for EC2\n# ----------------------------------------------------\nResources:\n  EC2Instance:\n    Type: AWS::EC2::Instance\n    Properties:\n      InstanceType: !Ref 'InstanceType'\n      SecurityGroups: [!Ref 'InstanceSecurityGroup']\n      KeyName: !Ref 'KeyName'\n      ImageId: !Ref 'LatestAmiId'\n      Tags:\n        - Key: Name\n          Value: CF-EC2\n\n# ----------------------------------------------------\n# Resource Creation for Security group\n# ----------------------------------------------------\n  InstanceSecurityGroup:\n    Type: AWS::EC2::SecurityGroup\n    Properties:\n      GroupDescription: Enable SSH access via port 22\n      SecurityGroupIngress:\n      - IpProtocol: tcp\n        FromPort: 22\n        ToPort: 22\n        CidrIp: !Ref 'SSHLocation'\n\n# ----------------------------------------------------\n# After stack creation outputs act like a terraform output\n# ----------------------------------------------------\nOutputs:\n  InstanceId:\n    Description: InstanceId of the newly created EC2 instance\n    Value: !Ref 'EC2Instance'\n  AZ:\n    Description: Availability Zone of the newly created EC2 instance\n    Value: !GetAtt [EC2Instance, AvailabilityZone]\n  PublicDNS:\n    Description: Public DNSName of the newly created EC2 instance\n    Value: !GetAtt [EC2Instance, PublicDnsName]\n  PublicIP:\n    Description: Public IP address of the newly created EC2 instance\n    Value: !GetAtt [EC2Instance, PublicIp]\n```\n\n_main.tf_\n```\n# ----------------------------------------------------\n# CloudFormation Stack\n# ----------------------------------------------------\nresource \"aws_cloudformation_stack\" \"ec2_instance\" {\n  name = \"ec2-instance-stack\"\n\n  parameters = {\n    KeyName = var.key_name\n    InstanceType = var.instance_type\n  }\n  template_body = file(\"${var.cf_file}\")\n\n  tags = tomap({\"Name\" = \"CF-EC2-Stack\"})\n}\n```\n\n----\n## Referance\n- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html\n- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudformation_stack\n- https://octopus.com/blog/aws-cloudformation-ec2-examples\n\n----\n## Conclusion\nThis is a combined infrastructure deployment using AWS CloudFormation and Terraform. and it's created for a try how to use hybrid deployment of IaC (Terraform, CloudFormation) \n\n### ⚙️ Connect with Me \n\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"mailto:yousaf.k.hamza@gmail.com\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gmail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white\"/\u003e\u003c/a\u003e \n\u003ca href=\"https://www.instagram.com/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge\u0026logo=instagram\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://wa.me/%2B917736720639?text=This%20message%20from%20GitHub.\"\u003e\u003cimg src=\"https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white\"/\u003e\u003c/a\u003e\u003cbr /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fcloudformation-stack-deployment-through-terraform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyousafkhamza%2Fcloudformation-stack-deployment-through-terraform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fcloudformation-stack-deployment-through-terraform/lists"}