{"id":4330,"date":"2024-12-30T14:23:24","date_gmt":"2024-12-30T14:23:24","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4330"},"modified":"2024-12-30T14:25:23","modified_gmt":"2024-12-30T14:25:23","slug":"terraform-aws-assign-pre-existing-elastic-ip-address-to-an-ec2-instance","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4330","title":{"rendered":"Terraform &#8211; AWS Assign Pre-existing Elastic IP Address to an EC2 Instance"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Elastic IP Addresses (EIP) are great as they provide you with a static IP address which you can use for an endpoint to your application, but when you are automating things with Terraform, you don&#8217;t want it to be continually re-created or that IP address will change, and if you have DNS records pointing at that IP address, you end up having to change them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the mark-up that allows you to import a pre-existing Elastic IP address, all you need to do is ensure that the EIP you create has specific tag set, which you can then specify in your Terraform.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create EIP manually, with name: <strong>VPC1_EIP1<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can specify it within your Terraform as below, in this example we&#8217;re allocating it to an EC2 Instance called: <strong>VPC1-Instance-1<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data \"aws_eip\" \"VPC1-EIP1\" {\n  filter {\n    name   = \"tag:Name\"\n    values = &#91;\"VPC1-EIP1\"]\n  }\n}\n\nresource \"aws_eip_association\" \"eip_assoc\" {\n  instance_id   = aws_instance.VPC1-Instance-1.id\n  allocation_id = data.aws_eip.VPC1-EIP1.id\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Elastic IP Addresses (EIP) are great as they provide you with a static IP address which you can use for an endpoint to your application, but when you are automating things with Terraform, you don&#8217;t want it to be continually re-created or that IP address will change, and if you have DNS records pointing at &#8230; <a title=\"Terraform &#8211; AWS Assign Pre-existing Elastic IP Address to an EC2 Instance\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4330\" aria-label=\"Read more about Terraform &#8211; AWS Assign Pre-existing Elastic IP Address to an EC2 Instance\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4332,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,45],"tags":[],"class_list":["post-4330","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-terraform"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4330"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4330\/revisions"}],"predecessor-version":[{"id":4331,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4330\/revisions\/4331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4332"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}