{"id":9693,"date":"2026-01-20T05:28:03","date_gmt":"2026-01-19T23:58:03","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=9693"},"modified":"2026-08-28T15:57:33","modified_gmt":"2026-08-28T10:27:33","slug":"how-to-create-an-aws-ec2-instance-using-terraform-in-ubuntu","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-create-an-aws-ec2-instance-using-terraform-in-ubuntu\/","title":{"rendered":"How to Create an AWS EC2 Instance Using Terraform in Ubuntu"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform is an infrastructure-as-code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Terraform<\/strong>, developed by HashiCorp, allows you to define and provision cloud resources using simple, declarative configuration files. With Terraform, infrastructure becomes <strong>version-controlled, repeatable, and automated<\/strong>, making it a core tool for <strong>DevOps engineers and system administrators<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before you begin, ensure the following requirements are met:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An <strong>active AWS account<\/strong><\/li>\n\n\n\n<li>An <strong>IAM user<\/strong> with:<br>Programmatic access<br>EC2, VPC, and Elastic IP permissions<\/li>\n\n\n\n<li>Ubuntu Linux<\/li>\n\n\n\n<li>Basic knowledge of:<br>Linux commands<br>AWS EC2 concepts<br>SSH access<br><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Install AWS CLI (Ubuntu)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform uses AWS credentials configured via the AWS CLI to authenticate with AWS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Update System Packages<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This step updates the local package index and installs required utilities (<code>curl<\/code> and <code>unzip<\/code>) that are needed to download and extract the AWS CLI installer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update\napt install -y curl unzip<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Download AWS CLI v2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Download the official AWS CLI v2 installer package from AWS to the local system.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o awscliv2.zip<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install AWS CLI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Extract the downloaded package and run the installation script to install AWS CLI v2 system-wide.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unzip awscliv2.zip\n.\/aws\/install<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Verify Installation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm that AWS CLI is installed successfully and check the installed version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws --version<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Configure AWS Credentials<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following command and provide your IAM credentials:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws configure<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enter:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>AWS Access Key ID<\/strong><\/li>\n\n\n\n<li><strong>AWS Secret Access Key<\/strong><\/li>\n\n\n\n<li><strong>Default region name:<\/strong> <code>ap-south-1<\/code><\/li>\n\n\n\n<li><strong>Default output format:<\/strong>json<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Install Terraform<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Add HashiCorp Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add HashiCorp\u2019s official GPG key and repository so Terraform can be installed and updated securely using <code>apt<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -O - https:\/\/apt.releases.hashicorp.com\/gpg | sudo gpg --dearmor -o \/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg\necho \"deb &#091;arch=$(dpkg --print-architecture) signed-by=\/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg] https:\/\/apt.releases.hashicorp.com $(grep -oP '(?&lt;=UBUNTU_CODENAME=).*' \/etc\/os-release || lsb_release -cs) main\" | sudo tee \/etc\/apt\/sources.list.d\/hashicorp.list\nsudo apt update &amp;&amp; sudo apt install terraform<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install Terraform<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Update the package list and install Terraform from the official HashiCorp repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update\napt install terraform -y<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Verify Installation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Verify that Terraform is installed correctly by checking its version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform -v<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Terraform Project Structure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a dedicated project directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir terraform-ec2 &amp;&amp; cd terraform-ec2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Project structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform-ec2\/<br>\u251c\u2500\u2500 provider.tf<br>\u251c\u2500\u2500 variables.tf<br>\u251c\u2500\u2500 main.tf<br>\u251c\u2500\u2500 outputs.tf<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Terraform Configuration Files<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>provider.tf<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>provider &#8220;aws&#8221; {<br>region = &#8220;ap-south-1&#8221;<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>variables.tf<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>variable &#8220;ami_id&#8221; {<br>default = &#8220;ami-0f5ee92e2d63afc18&#8221;<br>}<br>variable &#8220;instance_type&#8221; {<br>default = &#8220;t2.micro&#8221;<br>}<br>variable &#8220;key_name&#8221; {<br>default = &#8220;my-keypair&#8221;<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>main.tf<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>resource &#8220;aws_security_group&#8221; &#8220;ec2_sg&#8221; {<br>name = &#8220;terraform-sg&#8221;<br>ingress {<br>from_port = 22<br>to_port = 22<br>protocol = &#8220;tcp&#8221;<br>cidr_blocks = [&#8220;0.0.0.0\/0&#8221;]<br>}<br>ingress {<br>from_port = 80<br>to_port = 80<br>protocol = &#8220;tcp&#8221;<br>cidr_blocks = [&#8220;0.0.0.0\/0&#8221;]<br>}<br>egress {<br>from_port = 0<br>to_port = 0<br>protocol = &#8220;-1&#8221;<br>cidr_blocks = [&#8220;0.0.0.0\/0&#8221;]<br>}<br>}<br>resource &#8220;aws_instance&#8221; &#8220;ec2&#8221; {<br>ami = var.ami_id<br>instance_type = var.instance_type<br>key_name = var.key_name<br>vpc_security_group_ids = [aws_security_group.ec2_sg.id]<br>tags = {<br>Name = &#8220;Terraform-EC2&#8221;<br>}<br>}<br>resource &#8220;aws_eip&#8221; &#8220;eip&#8221; {<br>instance = aws_instance.ec2.id<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>outputs.tf<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>output &#8220;public_ip&#8221; {<br>value = aws_eip.eip.public_ip<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Deploy the Infrastructure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize Terraform:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform init<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"823\" height=\"305\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image.png\" alt=\"\" class=\"wp-image-9694\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image.png 823w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-300x111.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-768x285.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-810x300.png 810w\" sizes=\"(max-width: 823px) 100vw, 823px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Preview the execution plan:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform plan<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Apply the configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform apply<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-1.png\"><img decoding=\"async\" width=\"850\" height=\"275\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-1.png\" alt=\"\" class=\"wp-image-9695\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-1.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-1-300x97.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-1-768x248.png 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Type <strong>yes<\/strong> when prompted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verification<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Login to <strong>AWS Console<\/strong><\/li>\n\n\n\n<li>Navigate to <strong>EC2 \u2192 Instances<\/strong><\/li>\n\n\n\n<li>Verify:<br>Instance is running<br>Security group is attached<br>Elastic IP is assigned<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2.png\"><img decoding=\"async\" width=\"1024\" height=\"40\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-1024x40.png\" alt=\"\" class=\"wp-image-9696\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-1024x40.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-300x12.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-768x30.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-1536x59.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2-850x33.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2026\/01\/image-2.png 1577w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">SSH Access<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the private key (<code>.pem<\/code> file) associated with your EC2 instance to establish an SSH connection.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -i my-keypair.pem ubuntu@&lt;Elastic-IP&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Cleanup Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid unnecessary AWS charges, destroy all resources when no longer needed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform destroy<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform simplifies cloud infrastructure management by enabling <strong>automation, consistency, and scalability<\/strong>. In this guide, we successfully created an <strong>AWS EC2 instance using Terraform<\/strong>, complete with security groups and an Elastic IP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Terraform is an infrastructure-as-code tool that lets you define both cloud and on-prem resources in human-readable configuration files that [&hellip;]<\/p>\n","protected":false},"author":510,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1019],"tags":[1071,1064],"class_list":["post-9693","post","type-post","status-publish","format-standard","hentry","category-cloud-aws","tag-aws-ec2","tag-terraform","psol-cat-cloud-aws"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2wl","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/510"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=9693"}],"version-history":[{"count":3,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9693\/revisions"}],"predecessor-version":[{"id":9699,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9693\/revisions\/9699"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=9693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=9693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=9693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}