{"id":786,"date":"2023-07-17T06:20:00","date_gmt":"2023-07-17T06:20:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=786"},"modified":"2023-07-17T06:20:00","modified_gmt":"2023-07-17T06:20:00","slug":"terraform-iam-role","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=786","title":{"rendered":"How to Create IAM Role Using Terraform"},"content":{"rendered":"<p>In this guide, you will learn to Terraform IAM role creation using step by step guide.<\/p>\n<p>To create a IAM role for using it with the ec2 instance you need to do the following.<\/p>\n<ol>\n<li>Create IAM Policy with the required permission to AWS resources.<\/li>\n<li>Create IAM Role<\/li>\n<li>Attach the policy to the role.<\/li>\n<li>Create an instance profile and attach it to the role.<\/li>\n<\/ol>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note: <\/strong><\/b>Manually we attach <a href=\"https:\/\/devopscube.com\/aws-iam-role-instance-profile\/\">IAM Role<\/a> to instances. When you create instances through CLI or terraform, you need to attach a instance profile.<\/div>\n<\/div>\n<h2 id=\"terraform-iam-role-script\">Terraform IAM Role Script<\/h2>\n<p>Here is the full terraform script to create an IAM role and instance profile with policy.<\/p>\n<p>To use Terraform configuration for your specific needs, you&#8217;ll need to replace the names and policy statements highlighted in bold with values that suit your requirements. You can use the <a href=\"https:\/\/awspolicygen.s3.amazonaws.com\/policygen.html?ref=devopscube.com\" rel=\"noreferrer noopener\">AWS Policy Generator<\/a> to create the required policy document.<\/p>\n<pre><code>provider \"aws\" {\n  region = \"us-west-2\"\n}\n\nvariable \"instance_profile_name\" {\n  type    = string\n  default = \"example-instance-profile\"\n}\n\nvariable \"iam_policy_name\" {\n  type    = string\n  default = \"example-policy\"\n}\n\nvariable \"role_name\" {\n  type    = string\n  default = \"example-role\"\n}\n\n# Create an IAM policy\nresource \"aws_iam_policy\" \"jenkins_iam_policy\" {\n  name = var.iam_policy_name\n\n  policy = jsonencode({\n    Version = \"2012-10-17\"\n    Statement = [\n      {\n        Effect = \"Allow\"\n        Action = [\n          \"secretsmanager:GetSecretValue\",\n          \"ssm:GetParameter\",\n          \"ssm:GetParameters\",\n          \"ssm:GetParametersByPath\"\n        ]\n        Resource = \"*\"\n      }\n    ]\n  })\n}\n\n# Create an IAM role\nresource \"aws_iam_role\" \"jenkins_role\" {\n  name = var.role_name\n\n  assume_role_policy = jsonencode({\n    Version = \"2012-10-17\"\n    Statement = [\n      {\n        Effect = \"Allow\"\n        Principal = {\n          Service = \"ec2.amazonaws.com\"\n        }\n        Action = \"sts:AssumeRole\"\n      }\n    ]\n  })\n}\n\n# Attach the IAM policy to the IAM role\nresource \"aws_iam_policy_attachment\" \"jenkins_role_policy_attachment\" {\n  name = \"Policy Attachement\"\n  policy_arn = aws_iam_policy.jenkins_iam_policy.arn\n  roles       = [aws_iam_role.jenkins_role.name]\n}\n\n# Create an IAM instance profile\nresource \"aws_iam_instance_profile\" \"jenkins_instance_profile\" {\n  name = var.instance_profile_name\n  role = aws_iam_role.jenkins_role.name\n}<\/code><\/pre>\n<p>Additionally, you may want to consider converting this configuration into a module format for easier reuse and maintainability across your infrastructure.<\/p>\n<h2 id=\"create-iam-role-using-terraform\">Create IAM Role Using Terraform<\/h2>\n<p>To execute the script, first, you need to initialize Terraform from the folder you have the Terraform script.<\/p>\n<p>This will download the necessary provider plugins.<\/p>\n<pre><code>terraform init<\/code><\/pre>\n<p>Now execute the plan. It will show a summary of the actions Terraform intends to take and any potential issues or conflicts that it detects with your configuration.<\/p>\n<pre><code>terraform plan<\/code><\/pre>\n<p>If you are ok with the plan summary, you can apply the configuration using the following command.<\/p>\n<pre><code>terraform apply --auto-approve<\/code><\/pre>\n<h2 id=\"attach-instance-profile-to-ec2-instance\">Attach Instance Profile to ec2 Instance<\/h2>\n<p>Here is an example terraform script to attach an instance profile to the ec2 instance.<\/p>\n<p>Replace <strong>instance_profile_name<\/strong> with the instance profile name you added in the terraform iam role script.<\/p>\n<pre><code>resource \"aws_instance\" \"example_instance\" {\n  ami           = \"ami-1234567890\"\n  instance_type = \"t2.micro\"\n  key_name      = \"key_pair_name\"\n  subnet_id     = \"subnet-058a7514ba8adbb07\"\n\n  iam_instance_profile {\n    name = instance_profile_name\n  }\n\n  tags = {\n    Name = \"example_instance\"\n  }\n}<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we looked at IAM role provisioning using Terraform.<\/p>\n<p>If you are working on RDS, take a look at <a href=\"https:\/\/devopscube.com\/terraform-aws-rds\/\">Terraform AWS rds<\/a> provisioning guide.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/terraform-iam-role\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Create IAM Role Using Terraform \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/terraform-iam-role\/<\/p>\n","protected":false},"author":1,"featured_media":787,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=786"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/786\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/787"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}