How to just create a new version of AWS Appconfig Configuration Profile every time content of config file getting changed

114 Views Asked by At

I want to manage AWS appconfig via Terraform. But the problem I am facing is that terraform on every config change, it destroy/create the resource which config’s hosted configuration, which results in the same version. This is useless to anybody using Appconfig.

The idea is to create a newer version everytime content of file got changed and deploy it thorugh some other workflow.

Sample code I am trying with.

provider "aws" {
  region  = "ap-south-1"
  profile = "default"
}

# Create the AppConfig application
resource "aws_appconfig_application" "homepage" {
  name        = "homepage"
  description = "Homepage application configuration"
}

# Create the configuration profiles
# application.yml
resource "aws_appconfig_configuration_profile" "application" {
  application_id = aws_appconfig_application.homepage.id
  name           = "application"
  description    = "Application configuration profile"
  location_uri   = "hosted"
}

# http.yml
resource "aws_appconfig_configuration_profile" "http" {
  application_id = aws_appconfig_application.homepage.id
  name           = "http"
  description    = "HTTP configuration profile"
  location_uri   = "hosted"
}

# Create the environment
resource "aws_appconfig_environment" "uat" {
  application_id = aws_appconfig_application.homepage.id
  name           = "uat"
  description    = "UAT environment"
}

# Create the hosted configuration versions (assuming content is in local files)
resource "aws_appconfig_hosted_configuration_version" "application" {
  application_id           = aws_appconfig_application.homepage.id
  configuration_profile_id = aws_appconfig_configuration_profile.application.configuration_profile_id
  content                  = file("${path.module}/configs/application.yml")
  content_type             = "application/x-yaml"
  description              = "Application configuration version"
}

resource "aws_appconfig_hosted_configuration_version" "http" {
  application_id           = aws_appconfig_application.homepage.id
  configuration_profile_id = aws_appconfig_configuration_profile.http.configuration_profile_id
  content                  = file("${path.module}/configs/http.yml")
  content_type             = "application/x-yaml"
  description              = "HTTP configuration version"
}
1

There are 1 best solutions below

1
tommmlij On

You have to add a hosted_configuration_version to the terraform code and also a deployment, like this (my usecase is slightly different, but should be understandable):

resource "aws_appconfig_hosted_configuration_version" "click-relay-initial" {
  application_id           = aws_appconfig_application.click-relay.id
  configuration_profile_id = aws_appconfig_configuration_profile.click-relay.configuration_profile_id
  description              = "Initial version"
  content_type             = "text/plain"


  content = yamlencode({
    foo            = "bar"
  })
}

resource "aws_appconfig_hosted_configuration_version" "click-relay-v2" {
  application_id           = aws_appconfig_application.click-relay.id
  configuration_profile_id = aws_appconfig_configuration_profile.click-relay.configuration_profile_id
  description              = "Changed foo"
  content_type             = "text/plain"


  content = yamlencode({
    foo            = "world"
  })
}

resource "aws_appconfig_deployment" "click-relay-initial" {
  application_id           = aws_appconfig_application.click-relay.id
  configuration_profile_id = aws_appconfig_configuration_profile.click-relay.configuration_profile_id
  configuration_version    = aws_appconfig_hosted_configuration_version.click-relay-initial.version_number
  deployment_strategy_id   = aws_appconfig_deployment_strategy.click-relay.id
  description              = "Ad-hoc deployment"
  environment_id           = aws_appconfig_environment.click-relay.environment_id
}

resource "aws_appconfig_deployment" "click-relay-v2" {
  application_id           = aws_appconfig_application.click-relay.id
  configuration_profile_id = aws_appconfig_configuration_profile.click-relay.configuration_profile_id
  configuration_version    = aws_appconfig_hosted_configuration_version.click-relay-v2.version_number
  deployment_strategy_id   = aws_appconfig_deployment_strategy.click-relay.id
  description              = "Ad-hoc deployment"
  environment_id           = aws_appconfig_environment.click-relay.environment_id
}

This adds a new version.