From 7433be5b4cffee5876852f1fc363737b772eb573 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 17 Aug 2016 07:07:54 +0100 Subject: [PATCH] aws: Make EFS MT creation fail for 2+ targets per AZ (#8205) --- .../aws/resource_aws_efs_mount_target.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_efs_mount_target.go b/builtin/providers/aws/resource_aws_efs_mount_target.go index 34597230e..bc3ffbb93 100644 --- a/builtin/providers/aws/resource_aws_efs_mount_target.go +++ b/builtin/providers/aws/resource_aws_efs_mount_target.go @@ -67,9 +67,24 @@ func resourceAwsEfsMountTarget() *schema.Resource { func resourceAwsEfsMountTargetCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).efsconn + fsId := d.Get("file_system_id").(string) + subnetId := d.Get("subnet_id").(string) + + // CreateMountTarget would return the same Mount Target ID + // to parallel requests if they both include the same AZ + // and we would end up managing the same MT as 2 resources. + // So we make it fail by calling 1 request per AZ at a time. + az, err := getAzFromSubnetId(subnetId, meta.(*AWSClient).ec2conn) + if err != nil { + return fmt.Errorf("Failed getting AZ from subnet ID (%s): %s", subnetId, err) + } + mtKey := "efs-mt-" + fsId + "-" + az + awsMutexKV.Lock(mtKey) + defer awsMutexKV.Unlock(mtKey) + input := efs.CreateMountTargetInput{ - FileSystemId: aws.String(d.Get("file_system_id").(string)), - SubnetId: aws.String(d.Get("subnet_id").(string)), + FileSystemId: aws.String(fsId), + SubnetId: aws.String(subnetId), } if v, ok := d.GetOk("ip_address"); ok {