0
votes

Attempting to update a single key/value of a parameter in an existing Cloudformation template.

...

svc := cloudformation.New(AWSSession)

// Add parameters. Used in AWS CreateStack/UpdateStack call
var params []*cloudformation.Parameter
for key, val := range cfnparamsOverrides {
    log.Printf("Adding parameter key-> %v value-> %v", key, val)
    param := &cloudformation.Parameter{}
    param.ParameterKey = aws.String(key)
    param.ParameterValue = aws.String(val)
    params = append(params, param)
}

CreateChangeSetInput := &cloudformation.CreateChangeSetInput{
    StackName:     aws.String(stackARN),      // required field.
    ChangeSetName: aws.String(ChangeSetName), // must be a unique value
    ChangeSetType: aws.String("UPDATE"),
    // TemplateBody:  aws.String(templateBody),
    UsePreviousTemplate: aws.Bool(true),
    Parameters:          params}

// Issue AWS CreateChangeSet API call
log.Println("Issuing AWS CreateChangeSet() API call")
_, errCreateChangeSet := svc.CreateChangeSet(CreateChangeSetInput)
if errCreateChangeSet != nil {
    return false, fmt.Errorf("Got error on CreateChangeSet: %v", errCreateChangeSet.Error())
}

only 1 value is being set, but cloudformation template validation throws the following: ValidationError: Parameters: [Key1, Key2, Key3, ...] must have values

It seems to be requiring key/value for all the cloudformation paramters existing in the template. Is there a way to only provide the new/existing key/value pair I need updated?

Within the golang aws documentation I discovered the *cloudformation.Parameter struct has a UsePreviousValue for each value. However I don't see the use of this other than to avoid modifying the existing value, it still requires I provide the same paramaters keys in the deployed cfn template.

Am I missing something? It seems like a relatively straight forward approach to update an existing value in a deployed environment.

Wondering how others are going about this. Should I be querying the existing environment to read in all the parameters key/values, modify the 1 I need, and then submit changeset?

1

1 Answers

0
votes

I think using UsePreviousValue is the only way you can do this.

The same behavior is for AWS CLI where create-change-set or update-stack will require you to list all the parameters again and set the UsePreviousValue="true" for each of them.

If you want to avoid doing this all the time, you could create a wrapper around your CreateChangeSet which would automatically populate the parameters.

This limitation origins directly from the CloudFormation REST API. The example provided for the API, also explicitly lists all the parameters and uses UsePreviousValue to indicate a re-use of the value:

https://cloudformation.us-east-1.amazonaws.com/
 ?Action=CreateChangeSet
 &ChangeSetName=SampleChangeSet
 &Parameters.member.1.ParameterKey=KeyName
 &Parameters.member.1.UsePreviousValue=true
 &Parameters.member.2.ParameterKey=Purpose
 &Parameters.member.2.ParameterValue=production
 &StackName=arn:aws:cloudformation:us-east-1:123456789012:stack/SampleStack/1a2345b6-0000-00a0-a123-00abc0abc000
 &UsePreviousTemplate=true
 &Version=2010-05-15
 &X-Amz-Algorithm=AWS4-HMAC-SHA256
 &X-Amz-Credential=[Access key ID and scope]
 &X-Amz-Date=20160316T233349Z
 &X-Amz-SignedHeaders=content-type;host
 &X-Amz-Signature=[Signature]