{"componentChunkName":"component---src-gatsby-theme-chronoblog-templates-note-js","path":"/notes/infrastructure-as-code-iac-terraform/","result":{"data":{"mdx":{"parent":{"__typename":"File","fields":{"gitLogLatestDate":"2026-04-28 11:54:05 +0200"}},"id":"b52d57d5-5826-5fb1-be63-79c215795674","excerpt":"Infrastructure as Code (IaC) is a DevOps approach where infrastructure is defined and managed using code instead of manual setup. This makes…","frontmatter":{"title":"Infrastructure as Code (IaC) with Terraform (AWS EC2 Example)","date":"2026-04-28 09:52:00 UTC","job_ad":null,"job_ad_id":null,"job_ad_url":null,"tags":["devops","iac","terraform","aws","ec2"],"cover":null},"fields":{"slug":"/notes/infrastructure-as-code-iac-terraform/","readingTime":{"text":"2 min read"}},"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Infrastructure as Code (IaC) with Terraform (AWS EC2 Example)\",\n  \"date\": \"2026-04-28 09:52:00 UTC\",\n  \"tags\": [\"devops\", \"iac\", \"terraform\", \"aws\", \"ec2\"],\n  \"canonical_url\": \"https://sevic.dev/notes/infrastructure-as-code-iac-terraform/\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Infrastructure as Code (IaC) is a DevOps approach where infrastructure is defined and managed using code instead of manual setup. This makes environments reproducible, version-controlled, and easy to scale.\"), mdx(\"p\", null, \"In this guide, you'll provision an AWS EC2 instance using Terraform.\"), mdx(\"h3\", {\n    \"id\": \"requirements\"\n  }, \"Requirements\"), mdx(\"p\", null, \"Before starting, install:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Terraform  \"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"AWS CLI  \")), mdx(\"h3\", {\n    \"id\": \"aws-credentials-setup\"\n  }, \"AWS Credentials Setup\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Go to IAM \\u2192 Security credentials in AWS  \"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Create access keys  \"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Configure locally:\")), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-bash\"\n  }), \"aws configure\\n\")), mdx(\"p\", null, \"This stores credentials in:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"~/.aws/credentials\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"~/.aws/config\"))), mdx(\"h3\", {\n    \"id\": \"project-structure\"\n  }, \"Project Structure\"), mdx(\"p\", null, \"A simple Terraform setup:\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \".\\n\\u251C\\u2500\\u2500 main.tf\\n\\u251C\\u2500\\u2500 variables.tf\\n\\u251C\\u2500\\u2500 terraform.tfvars\\n\\u251C\\u2500\\u2500 outputs.tf\\n\")), mdx(\"h3\", {\n    \"id\": \"provider-configuration\"\n  }, \"Provider Configuration\"), mdx(\"p\", null, \"Define your cloud provider in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main.tf\"), \":\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \"provider \\\"aws\\\" {\\n  profile = \\\"default\\\"\\n  region  = \\\"eu-north-1\\\"\\n}\\n\")), mdx(\"h3\", {\n    \"id\": \"variables\"\n  }, \"Variables\"), mdx(\"p\", null, \"Define reusable variables in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"variables.tf\"), \":\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \"variable \\\"instance_name\\\" {\\n  description = \\\"Name tag for EC2 instance\\\"\\n  type        = string\\n  default     = \\\"MyNewInstance\\\"\\n}\\n\\nvariable \\\"ec2_instance_type\\\" {\\n  description = \\\"EC2 instance type\\\"\\n  type        = string\\n  default     = \\\"t3.micro\\\"\\n}\\n\")), mdx(\"p\", null, \"Set values in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"terraform.tfvars\"), \":\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \"instance_name      = \\\"MyEC2Name\\\"\\nec2_instance_type  = \\\"t3.micro\\\"\\n\")), mdx(\"h3\", {\n    \"id\": \"ec2-instance-configuration\"\n  }, \"EC2 Instance Configuration\"), mdx(\"p\", null, \"Add the resource in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"main.tf\"), \":\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \"resource \\\"aws_instance\\\" \\\"app_server\\\" {\\n  ami           = \\\"ami-077d1b9f9a1902bbc\\\"\\n  instance_type = var.ec2_instance_type\\n\\n  tags = {\\n    Name = var.instance_name\\n  }\\n}\\n\")), mdx(\"p\", null, \"You can find AMI IDs in EC2 \\u2192 Images \\u2192 AMI Catalog.\"), mdx(\"h3\", {\n    \"id\": \"outputs\"\n  }, \"Outputs\"), mdx(\"p\", null, \"Expose useful data in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"outputs.tf\"), \":\"), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {}), \"output \\\"instance_id\\\" {\\n  description = \\\"EC2 instance ID\\\"\\n  value       = aws_instance.app_server.id\\n}\\n\\noutput \\\"instance_public_ip\\\" {\\n  description = \\\"Public IP address\\\"\\n  value       = aws_instance.app_server.public_ip\\n}\\n\")), mdx(\"h3\", {\n    \"id\": \"terraform-workflow\"\n  }, \"Terraform Workflow\"), mdx(\"p\", null, \"Initialize the project:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"terraform init\"))), mdx(\"p\", null, \"Preview changes:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"terraform plan\"))), mdx(\"p\", null, \"Apply changes:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"terraform apply\"))), mdx(\"p\", null, \"Destroy infrastructure:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"terraform destroy\"))), mdx(\"h3\", {\n    \"id\": \"important-notes\"\n  }, \"Important Notes\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"State file\")), mdx(\"p\", null, \"Terraform stores infrastructure state in \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"terraform.tfstate\"), \".\"), mdx(\"p\", null, \"Do not commit this file to Git.\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Remote state (recommended)\")), mdx(\"p\", null, \"For teams, store state in S3 with locking via DynamoDB.\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Idempotency\")), mdx(\"p\", null, \"Running \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"apply\"), \" multiple times won\\u2019t recreate resources unnecessarily.\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Version control\")), mdx(\"p\", null, \"Treat Terraform code like application code.\"));\n}\n;\nMDXContent.isMDXComponent = true;"}},"pageContext":{"id":"b52d57d5-5826-5fb1-be63-79c215795674"}},"staticQueryHashes":["1961101537","2542493696"]}