By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Adding origin to existing Cloudfront Distribution

0

I have an already created cloudfront distribtuion. i want to attach a api gateway as a origin to it from terraform. But there is no resource dedicated to origin. the cloud front distribution is created in another project by someone else. i have to add to that distribution from my project. there are already number of origin already added to that distribution how to solve this

1 Answer
0

To add an API Gateway as an origin to an existing CloudFront distribution using Terraform, when the distribution was created in another project, you can follow these steps:

  1. First, you'll need to import the existing CloudFront distribution into your Terraform state. This allows you to manage the distribution without recreating it. Use the terraform import command to do this.

  2. Once the distribution is in your Terraform state, you can use the aws_cloudfront_distribution resource to manage it. You'll need to define the entire distribution configuration in your Terraform code, including the existing origins and the new API Gateway origin you want to add.

  3. To add the new API Gateway origin, you'll include it in the origin block within the aws_cloudfront_distribution resource. Each origin in CloudFront is part of the distribution configuration, not a separate resource.

  4. In your Terraform configuration, you'll need to specify all the existing origins along with the new API Gateway origin. This ensures that you don't accidentally remove any existing origins when you apply your changes.

  5. For the API Gateway origin, you'll need to provide details such as the domain name of your API Gateway, the origin path (if any), and any custom headers required.

  6. You may also need to create a new cache behavior for the API Gateway origin, depending on your requirements.

  7. After defining the updated distribution configuration with the new origin, you can apply your Terraform changes. This will update the existing CloudFront distribution to include the new API Gateway origin.

Remember to coordinate with the team managing the original distribution, as changes you make could impact their setup. Also, be cautious when modifying an existing distribution, as it can affect the availability of your content during the update process.

It's important to note that while this approach allows you to add the new origin, it also means you're taking over management of the entire distribution in your Terraform code. Ensure you have a complete and accurate representation of the existing distribution configuration before applying any changes.
Sources
Create a distribution - Amazon CloudFront
Use various origins with CloudFront distributions - Amazon CloudFront

profile picture
answered a month ago
  • So in this case the CloudFront distribution state is updated the with the Api gateway. so if i want to delete only the Api-gateway origin is it possible. or will the whole distribution get deleted? i Want to create and delete Api gateway origin to already existing cloud front distribution, how to achieve this

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions