Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CREATING_SNAPSHOT status while waiting for MWAA version upgrade #31833

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/31833.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_mwaa_environment: Consider `CREATING_SNAPSHOT` a valid pending state for resource update
```

```release-note:note
resource/aws_mwaa_environment: Upgrading your environment to a new major version of Apache Airflow forces replacement of the resource
```
26 changes: 22 additions & 4 deletions internal/service/mwaa/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mwaa"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -264,7 +266,25 @@ func ResourceEnvironment() *schema.Resource {
},
},

CustomizeDiff: verify.SetTagsDiff,
CustomizeDiff: customdiff.Sequence(
customdiff.ForceNewIf("airflow_version", func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool {
o, n := d.GetChange("airflow_version")

if oldVersion, err := gversion.NewVersion(o.(string)); err == nil {
if newVersion, err := gversion.NewVersion(n.(string)); err == nil {
// https://docs.aws.amazon.com/mwaa/latest/userguide/airflow-versions.html#airflow-versions-upgrade:
// Amazon MWAA supports minor version upgrades.
// This means you can upgrade your environment from version x.4.z to x.5.z.
// However, you cannot upgrade your environment to a new major version of Apache Airflow.
// For example, upgrading from version 1.y.z to 2.y.z is not supported.
return oldVersion.Segments()[0] < newVersion.Segments()[0]
}
}

return false
}),
verify.SetTagsDiff,
),
}
}

Expand Down Expand Up @@ -346,7 +366,6 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, meta
input.WeeklyMaintenanceWindowStart = aws.String(v.(string))
}

log.Printf("[INFO] Creating MWAA Environment: %s", input)
/*
Execution roles created just before the MWAA Environment may result in ValidationExceptions
due to IAM permission propagation delays.
Expand Down Expand Up @@ -511,7 +530,6 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta
input.WeeklyMaintenanceWindowStart = aws.String(d.Get("weekly_maintenance_window_start").(string))
}

log.Printf("[INFO] Updating MWAA Environment: %s", input)
_, err := conn.UpdateEnvironmentWithContext(ctx, input)

if err != nil {
Expand Down Expand Up @@ -635,7 +653,7 @@ func waitEnvironmentCreated(ctx context.Context, conn *mwaa.MWAA, name string, t

func waitEnvironmentUpdated(ctx context.Context, conn *mwaa.MWAA, name string, timeout time.Duration) (*mwaa.Environment, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{mwaa.EnvironmentStatusUpdating},
Pending: []string{mwaa.EnvironmentStatusUpdating, mwaa.EnvironmentStatusCreatingSnapshot},
Target: []string{mwaa.EnvironmentStatusAvailable},
Refresh: statusEnvironment(ctx, conn, name),
Timeout: timeout,
Expand Down
81 changes: 75 additions & 6 deletions internal/service/mwaa/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package mwaa_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mwaa"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -150,7 +152,7 @@ func TestAccMWAAEnvironment_airflowOptions(t *testing.T) {

func TestAccMWAAEnvironment_log(t *testing.T) {
ctx := acctest.Context(t)
var environment mwaa.Environment
var environment1, environment2 mwaa.Environment
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_mwaa_environment.test"

Expand All @@ -163,7 +165,7 @@ func TestAccMWAAEnvironment_log(t *testing.T) {
{
Config: testAccEnvironmentConfig_logging(rName, "true", mwaa.LoggingLevelCritical),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment),
testAccCheckEnvironmentExists(ctx, resourceName, &environment1),
resource.TestCheckResourceAttr(resourceName, "logging_configuration.#", "1"),

resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.dag_processing_logs.#", "1"),
Expand Down Expand Up @@ -200,7 +202,8 @@ func TestAccMWAAEnvironment_log(t *testing.T) {
{
Config: testAccEnvironmentConfig_logging(rName, "false", mwaa.LoggingLevelInfo),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment),
testAccCheckEnvironmentExists(ctx, resourceName, &environment2),
testAccCheckEnvironmentNotRecreated(&environment2, &environment1),
resource.TestCheckResourceAttr(resourceName, "logging_configuration.#", "1"),

resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.dag_processing_logs.#", "1"),
Expand Down Expand Up @@ -311,7 +314,7 @@ func TestAccMWAAEnvironment_full(t *testing.T) {

func TestAccMWAAEnvironment_pluginsS3ObjectVersion(t *testing.T) {
ctx := acctest.Context(t)
var environment mwaa.Environment
var environment1, environment2 mwaa.Environment
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_mwaa_environment.test"
s3ObjectResourceName := "aws_s3_object.plugins"
Expand All @@ -325,7 +328,7 @@ func TestAccMWAAEnvironment_pluginsS3ObjectVersion(t *testing.T) {
{
Config: testAccEnvironmentConfig_pluginsS3ObjectVersion(rName, "test"),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment),
testAccCheckEnvironmentExists(ctx, resourceName, &environment1),
resource.TestCheckResourceAttrPair(resourceName, "plugins_s3_object_version", s3ObjectResourceName, "version_id"),
),
},
Expand All @@ -337,7 +340,8 @@ func TestAccMWAAEnvironment_pluginsS3ObjectVersion(t *testing.T) {
{
Config: testAccEnvironmentConfig_pluginsS3ObjectVersion(rName, "test-updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment),
testAccCheckEnvironmentExists(ctx, resourceName, &environment2),
testAccCheckEnvironmentNotRecreated(&environment2, &environment1),
resource.TestCheckResourceAttrPair(resourceName, "plugins_s3_object_version", s3ObjectResourceName, "version_id"),
),
},
Expand All @@ -350,6 +354,42 @@ func TestAccMWAAEnvironment_pluginsS3ObjectVersion(t *testing.T) {
})
}

func TestAccMWAAEnvironment_updateAirflowVersionMinor(t *testing.T) {
ctx := acctest.Context(t)
var environment1, environment2 mwaa.Environment
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_mwaa_environment.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, mwaa.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckEnvironmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccEnvironmentConfig_airflowVersion(rName, "2.4.3"),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment1),
resource.TestCheckResourceAttr(resourceName, "airflow_version", "2.4.3"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccEnvironmentConfig_airflowVersion(rName, "2.5.1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckEnvironmentExists(ctx, resourceName, &environment2),
testAccCheckEnvironmentNotRecreated(&environment2, &environment1),
resource.TestCheckResourceAttr(resourceName, "airflow_version", "2.5.1"),
),
},
},
})
}

func testAccCheckEnvironmentExists(ctx context.Context, n string, v *mwaa.Environment) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -401,6 +441,16 @@ func testAccCheckEnvironmentDestroy(ctx context.Context) resource.TestCheckFunc
}
}

func testAccCheckEnvironmentNotRecreated(i, j *mwaa.Environment) resource.TestCheckFunc {
return func(s *terraform.State) error {
if !i.CreatedAt.Equal(aws.TimeValue(j.CreatedAt)) {
return errors.New("MWAA Environment was recreated")
}

return nil
}
}

func testAccEnvironmentConfig_base(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down Expand Up @@ -833,3 +883,22 @@ resource "aws_s3_object" "plugins" {
}
`, rName, content))
}

func testAccEnvironmentConfig_airflowVersion(rName, airflowVersion string) string {
return acctest.ConfigCompose(testAccEnvironmentConfig_base(rName), fmt.Sprintf(`
resource "aws_mwaa_environment" "test" {
dag_s3_path = aws_s3_object.dags.key
execution_role_arn = aws_iam_role.test.arn
name = %[1]q

network_configuration {
security_group_ids = [aws_security_group.test.id]
subnet_ids = aws_subnet.private[*].id
}

source_bucket_arn = aws_s3_bucket.test.arn

airflow_version = %[2]q
}
`, rName, airflowVersion))
}