Reference variables from another Terraform plan The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceTerraform: programmatically generate multiple routing tables for multiple VPC peering connectionsterraform remote stateTerraform and VPC PeeringTerraform Stackterraform - access remote state outputs from command lineTerragrunt v0.14.9, Terraform v0.11.7 reading AWS VPC ID from second environmentecs instances not created with terraformUsing two different roles to build resources in one terraform codeTerraform to create multiple vpc by re-executing same main.tfTerraform can't create a CloudFront's origin with a static S3 website endpoint

Difference between "generating set" and free product?

How is simplicity better than precision and clarity in prose?

Can the prologue be the backstory of your main character?

What is this lever in Argentinian toilets?

How to delete random line from file using Unix command?

Who or what is the being for whom Being is a question for Heidegger?

What aspect of planet Earth must be changed to prevent the industrial revolution?

Python - Fishing Simulator

Can undead you have reanimated wait inside a portable hole?

Mortgage adviser recommends a longer term than necessary combined with overpayments

Scientific Reports - Significant Figures

How do you keep chess fun when your opponent constantly beats you?

Do warforged have souls?

Wall plug outlet change

How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?

Do working physicists consider Newtonian mechanics to be "falsified"?

Does Parliament hold absolute power in the UK?

Arduino Pro Micro - switch off LEDs

What can I do if neighbor is blocking my solar panels intentionally?

How does ice melt when immersed in water?

University's motivation for having tenure-track positions

Road tyres vs "Street" tyres for charity ride on MTB Tandem

"... to apply for a visa" or "... and applied for a visa"?

Take groceries in checked luggage



Reference variables from another Terraform plan



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceTerraform: programmatically generate multiple routing tables for multiple VPC peering connectionsterraform remote stateTerraform and VPC PeeringTerraform Stackterraform - access remote state outputs from command lineTerragrunt v0.14.9, Terraform v0.11.7 reading AWS VPC ID from second environmentecs instances not created with terraformUsing two different roles to build resources in one terraform codeTerraform to create multiple vpc by re-executing same main.tfTerraform can't create a CloudFront's origin with a static S3 website endpoint



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I have created a set-up with main and disaster recovery website architecture in AWS using Terraform.



The main website is in region1 and disaster recovery is in region2. This script is created as different plans or different directories.



For region1, I created one directory which contains only the main website Terraform script to launch the main website infrastructure.



For region2, I created another directory which contains only the disaster recovery website Terraform script to launch the disaster recovery website infrastructure.



In my main website script, I need some values of the disaster recovery website such as VPC peering connection ID, DMS endpoint ARNs etc.



How can I reference these variables from the disaster recovery website directory to the main website directory?










share|improve this question






























    -1















    I have created a set-up with main and disaster recovery website architecture in AWS using Terraform.



    The main website is in region1 and disaster recovery is in region2. This script is created as different plans or different directories.



    For region1, I created one directory which contains only the main website Terraform script to launch the main website infrastructure.



    For region2, I created another directory which contains only the disaster recovery website Terraform script to launch the disaster recovery website infrastructure.



    In my main website script, I need some values of the disaster recovery website such as VPC peering connection ID, DMS endpoint ARNs etc.



    How can I reference these variables from the disaster recovery website directory to the main website directory?










    share|improve this question


























      -1












      -1








      -1








      I have created a set-up with main and disaster recovery website architecture in AWS using Terraform.



      The main website is in region1 and disaster recovery is in region2. This script is created as different plans or different directories.



      For region1, I created one directory which contains only the main website Terraform script to launch the main website infrastructure.



      For region2, I created another directory which contains only the disaster recovery website Terraform script to launch the disaster recovery website infrastructure.



      In my main website script, I need some values of the disaster recovery website such as VPC peering connection ID, DMS endpoint ARNs etc.



      How can I reference these variables from the disaster recovery website directory to the main website directory?










      share|improve this question
















      I have created a set-up with main and disaster recovery website architecture in AWS using Terraform.



      The main website is in region1 and disaster recovery is in region2. This script is created as different plans or different directories.



      For region1, I created one directory which contains only the main website Terraform script to launch the main website infrastructure.



      For region2, I created another directory which contains only the disaster recovery website Terraform script to launch the disaster recovery website infrastructure.



      In my main website script, I need some values of the disaster recovery website such as VPC peering connection ID, DMS endpoint ARNs etc.



      How can I reference these variables from the disaster recovery website directory to the main website directory?







      amazon-web-services amazon-ec2 terraform terraform-provider-aws aws-dms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 13:52









      ydaetskcoR

      23.4k46380




      23.4k46380










      asked Mar 22 at 6:02









      Mohamed JawadMohamed Jawad

      64




      64






















          1 Answer
          1






          active

          oldest

          votes


















          2














          One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this:



          vpc/main.tf



          resource "aws_vpc" "foo" 
          cidr_block = "10.0.0.0/16"


          output "vpc_id"
          value = "$aws_vpc.foo.id"



          route/main.tf



          data "terraform_remote_state" "vpc" 
          backend = "s3"
          config
          bucket = "mybucket"
          key = "path/to/my/key"
          region = "us-east-1"



          resource "aws_route_table" "rt"
          vpc_id = "$data.terraform_remote_state.vpc.vpc_id"



          However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need.



          So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this:



          data "aws_vpc_peering_connection" "pc" 
          vpc_id = "$data.aws_vpc.foo.id"
          peer_cidr_block = "10.0.0.0/16"


          resource "aws_route_table" "rt"
          vpc_id = "$aws_vpc.foo.id"


          resource "aws_route" "r"
          route_table_id = "$aws_route_table.rt.id"
          destination_cidr_block = "$data.aws_vpc_peering_connection.pc.peer_cidr_block"
          vpc_peering_connection_id = "$data.aws_vpc_peering_connection.pc.id"



          You'll need to do similar things for any other IDs or things you need to reference in your DR region.



          It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID, database names and potentially KMS key IDs which can all be done via data sources.






          share|improve this answer

























          • Thanks@ydaetskcoR,

            – Mohamed Jawad
            Mar 22 at 13:34











          • For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

            – Mohamed Jawad
            Mar 22 at 13:35






          • 1





            Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

            – ydaetskcoR
            Mar 22 at 13:51











          • Thanks@ydaetskcoR, for the reply. it's not working

            – Mohamed Jawad
            Mar 24 at 7:22











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293749%2freference-variables-from-another-terraform-plan%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this:



          vpc/main.tf



          resource "aws_vpc" "foo" 
          cidr_block = "10.0.0.0/16"


          output "vpc_id"
          value = "$aws_vpc.foo.id"



          route/main.tf



          data "terraform_remote_state" "vpc" 
          backend = "s3"
          config
          bucket = "mybucket"
          key = "path/to/my/key"
          region = "us-east-1"



          resource "aws_route_table" "rt"
          vpc_id = "$data.terraform_remote_state.vpc.vpc_id"



          However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need.



          So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this:



          data "aws_vpc_peering_connection" "pc" 
          vpc_id = "$data.aws_vpc.foo.id"
          peer_cidr_block = "10.0.0.0/16"


          resource "aws_route_table" "rt"
          vpc_id = "$aws_vpc.foo.id"


          resource "aws_route" "r"
          route_table_id = "$aws_route_table.rt.id"
          destination_cidr_block = "$data.aws_vpc_peering_connection.pc.peer_cidr_block"
          vpc_peering_connection_id = "$data.aws_vpc_peering_connection.pc.id"



          You'll need to do similar things for any other IDs or things you need to reference in your DR region.



          It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID, database names and potentially KMS key IDs which can all be done via data sources.






          share|improve this answer

























          • Thanks@ydaetskcoR,

            – Mohamed Jawad
            Mar 22 at 13:34











          • For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

            – Mohamed Jawad
            Mar 22 at 13:35






          • 1





            Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

            – ydaetskcoR
            Mar 22 at 13:51











          • Thanks@ydaetskcoR, for the reply. it's not working

            – Mohamed Jawad
            Mar 24 at 7:22















          2














          One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this:



          vpc/main.tf



          resource "aws_vpc" "foo" 
          cidr_block = "10.0.0.0/16"


          output "vpc_id"
          value = "$aws_vpc.foo.id"



          route/main.tf



          data "terraform_remote_state" "vpc" 
          backend = "s3"
          config
          bucket = "mybucket"
          key = "path/to/my/key"
          region = "us-east-1"



          resource "aws_route_table" "rt"
          vpc_id = "$data.terraform_remote_state.vpc.vpc_id"



          However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need.



          So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this:



          data "aws_vpc_peering_connection" "pc" 
          vpc_id = "$data.aws_vpc.foo.id"
          peer_cidr_block = "10.0.0.0/16"


          resource "aws_route_table" "rt"
          vpc_id = "$aws_vpc.foo.id"


          resource "aws_route" "r"
          route_table_id = "$aws_route_table.rt.id"
          destination_cidr_block = "$data.aws_vpc_peering_connection.pc.peer_cidr_block"
          vpc_peering_connection_id = "$data.aws_vpc_peering_connection.pc.id"



          You'll need to do similar things for any other IDs or things you need to reference in your DR region.



          It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID, database names and potentially KMS key IDs which can all be done via data sources.






          share|improve this answer

























          • Thanks@ydaetskcoR,

            – Mohamed Jawad
            Mar 22 at 13:34











          • For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

            – Mohamed Jawad
            Mar 22 at 13:35






          • 1





            Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

            – ydaetskcoR
            Mar 22 at 13:51











          • Thanks@ydaetskcoR, for the reply. it's not working

            – Mohamed Jawad
            Mar 24 at 7:22













          2












          2








          2







          One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this:



          vpc/main.tf



          resource "aws_vpc" "foo" 
          cidr_block = "10.0.0.0/16"


          output "vpc_id"
          value = "$aws_vpc.foo.id"



          route/main.tf



          data "terraform_remote_state" "vpc" 
          backend = "s3"
          config
          bucket = "mybucket"
          key = "path/to/my/key"
          region = "us-east-1"



          resource "aws_route_table" "rt"
          vpc_id = "$data.terraform_remote_state.vpc.vpc_id"



          However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need.



          So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this:



          data "aws_vpc_peering_connection" "pc" 
          vpc_id = "$data.aws_vpc.foo.id"
          peer_cidr_block = "10.0.0.0/16"


          resource "aws_route_table" "rt"
          vpc_id = "$aws_vpc.foo.id"


          resource "aws_route" "r"
          route_table_id = "$aws_route_table.rt.id"
          destination_cidr_block = "$data.aws_vpc_peering_connection.pc.peer_cidr_block"
          vpc_peering_connection_id = "$data.aws_vpc_peering_connection.pc.id"



          You'll need to do similar things for any other IDs or things you need to reference in your DR region.



          It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID, database names and potentially KMS key IDs which can all be done via data sources.






          share|improve this answer















          One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this:



          vpc/main.tf



          resource "aws_vpc" "foo" 
          cidr_block = "10.0.0.0/16"


          output "vpc_id"
          value = "$aws_vpc.foo.id"



          route/main.tf



          data "terraform_remote_state" "vpc" 
          backend = "s3"
          config
          bucket = "mybucket"
          key = "path/to/my/key"
          region = "us-east-1"



          resource "aws_route_table" "rt"
          vpc_id = "$data.terraform_remote_state.vpc.vpc_id"



          However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need.



          So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this:



          data "aws_vpc_peering_connection" "pc" 
          vpc_id = "$data.aws_vpc.foo.id"
          peer_cidr_block = "10.0.0.0/16"


          resource "aws_route_table" "rt"
          vpc_id = "$aws_vpc.foo.id"


          resource "aws_route" "r"
          route_table_id = "$aws_route_table.rt.id"
          destination_cidr_block = "$data.aws_vpc_peering_connection.pc.peer_cidr_block"
          vpc_peering_connection_id = "$data.aws_vpc_peering_connection.pc.id"



          You'll need to do similar things for any other IDs or things you need to reference in your DR region.



          It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID, database names and potentially KMS key IDs which can all be done via data sources.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 22 at 13:49

























          answered Mar 22 at 8:52









          ydaetskcoRydaetskcoR

          23.4k46380




          23.4k46380












          • Thanks@ydaetskcoR,

            – Mohamed Jawad
            Mar 22 at 13:34











          • For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

            – Mohamed Jawad
            Mar 22 at 13:35






          • 1





            Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

            – ydaetskcoR
            Mar 22 at 13:51











          • Thanks@ydaetskcoR, for the reply. it's not working

            – Mohamed Jawad
            Mar 24 at 7:22

















          • Thanks@ydaetskcoR,

            – Mohamed Jawad
            Mar 22 at 13:34











          • For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

            – Mohamed Jawad
            Mar 22 at 13:35






          • 1





            Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

            – ydaetskcoR
            Mar 22 at 13:51











          • Thanks@ydaetskcoR, for the reply. it's not working

            – Mohamed Jawad
            Mar 24 at 7:22
















          Thanks@ydaetskcoR,

          – Mohamed Jawad
          Mar 22 at 13:34





          Thanks@ydaetskcoR,

          – Mohamed Jawad
          Mar 22 at 13:34













          For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

          – Mohamed Jawad
          Mar 22 at 13:35





          For VPC Peering I use this method, but am not understanding the remote_state. Could you please send me one example

          – Mohamed Jawad
          Mar 22 at 13:35




          1




          1





          Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

          – ydaetskcoR
          Mar 22 at 13:51





          Added a remote state data source example. I'd strongly recommend using the provider native data sources for everything instead of relying on remote state though. Back before data sources were a thing people had to either hard-code IDs (sometimes in large lookup maps) or used remote state to fetch IDs. Now that there are data sources for most resources in the AWS provider you should use this where possible.

          – ydaetskcoR
          Mar 22 at 13:51













          Thanks@ydaetskcoR, for the reply. it's not working

          – Mohamed Jawad
          Mar 24 at 7:22





          Thanks@ydaetskcoR, for the reply. it's not working

          – Mohamed Jawad
          Mar 24 at 7:22



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293749%2freference-variables-from-another-terraform-plan%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript