DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Terraform Drift Detection at Scale: How to Catch Configuration Drift Early
  • Codify Your Cloud and Kubernetes With Crossplane and IaC
  • Terraform State File: Key Challenges and Solutions
  • Implement Amazon S3 Cross-Region Replication With Terraform

Trending

  • Secure by Design: Modernizing Authentication With Centralized Access and Adaptive Signals
  • Kullback–Leibler Divergence: Theory, Applications, and Implications
  • A Complete Guide to Modern AI Developer Tools
  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Let's Play With Terraform External Providers

Let's Play With Terraform External Providers

These external providers cannot all called with the same method, so here's how we can switch it up.

By 
Aurelie Vache user avatar
Aurelie Vache
·
Dec. 21, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
23.1K Views

Join the DZone community and get the full member experience.

Join For Free

With Terraform, in order to use an existing resource in a new resource we want to create, we can use a data source. It's powerful and easy to understand and manipulate, but the thing to know is that you can't retrieve every data source type as you want. Indeed, several data source types can't be retrieved with filter, but several another can.

For example, you can retrieve an existing VPC by its tag name:

# VPC
data "aws_vpc" "vpc-dzone" {
  filter {
    name = "tag:Name"
    values = ["myvpc-for-dzone-article"]
 }
}


It's cool, useful, but unfortunately, this filtering is not possible for every resource.

In my case, I needed to retrieve an existing (network) load balancer, in AWS, and then give the information in order to create an AWS Route 53.

So the solution is to:

1. Create a script which retrieves the resource

2. Use the result of the script in a Terraform data source, with an external provider

3. Use this special data in the resource you want to create

1. The Script

my_module/scripts/get_elb.sh :

#!/bin/bash
set -e
kubectl get svc mygateway -n istio-system -o json | jq .status.loadBalancer.ingress[0]


This script returns a JSON object with a Kubernetes (Istio) gateway (which is an AWS Load Balancer):

$ ./my_module/scripts/get_elb.sh 
{
"hostname": "a123456789gsfgfsgfsg12134gsgsg78-123456789dfdsf45545fdsf.elb.eu-central-1.amazonaws.com"
}


2. The Data Source with Terraform External Provider

my_module/data.tf :

data"external""elb"{

     program= ["bash", "${path.module}/scripts/get_elb.sh"]

}


3. The Definition of Wanted Resource

my_module/aws_r53.tf:

resource "aws_route53_record""mymodule_CNAME" {
    zone_id="${data.aws_route53_zone.my_zone.zone_id}"
    name="${var.domain_name}"
    type="CNAME"
    records= ["${data.external.elb.result.hostname}"]
    ttl="${var.route53_ttl}"
}


Finally, apply your Terraform resources definition in order to create your resources.

You can now check providers used by Terraform in your project & modules:

$ terraform providers
.
├── provider.aws ~> 1.24.0
└── module.my_module
├── provider.aws (inherited)
└── provider.external


That's it!

Terraform (software)

Opinions expressed by DZone contributors are their own.

Related

  • Terraform Drift Detection at Scale: How to Catch Configuration Drift Early
  • Codify Your Cloud and Kubernetes With Crossplane and IaC
  • Terraform State File: Key Challenges and Solutions
  • Implement Amazon S3 Cross-Region Replication With Terraform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!