-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package se.solrike.cloudformation | ||
|
||
import javax.annotation.Nullable | ||
|
||
import software.amazon.awssdk.services.ssm.SsmClient | ||
import software.amazon.awssdk.services.ssm.model.ParameterType | ||
import software.amazon.awssdk.services.ssm.paginators.GetParametersByPathIterable | ||
|
||
/** | ||
* Utilities for AWS SSM (Simple system management) like Parameter Store | ||
*/ | ||
public class SsmUtils { | ||
|
||
public static void uploadParameters(Map<String, String> parameters, String excludeFilter, | ||
@Nullable String parameterPrefix = null) { | ||
SsmClient client = SsmClient.builder().build() | ||
|
||
parameters.findAll { !it.key.startsWith(excludeFilter) && it.value }.each { name, value -> | ||
client.putParameter { | ||
it.name(parameterPrefix ? "/$parameterPrefix/$name" : "/$name") | ||
it.value(value) | ||
it.overwrite(true) | ||
it.type(encryptParameter(name) ? ParameterType.SECURE_STRING : ParameterType.STRING) | ||
} | ||
} | ||
} | ||
|
||
public static void printParameters(String parameterPrefix) { | ||
SsmClient client = SsmClient.builder().build() | ||
List<String> parameters = [] | ||
|
||
GetParametersByPathIterable iterable = client.getParametersByPathPaginator { | ||
it.path('/' + parameterPrefix) | ||
it.withDecryption(true) | ||
} | ||
iterable.forEach { | ||
it.parameters.each { parameters.add("$it.name : $it.value") } | ||
} | ||
parameters.sort().each { println it} | ||
} | ||
|
||
|
||
static boolean encryptParameter(String name) { | ||
return (name.endsWithIgnoreCase('password') || name.endsWithIgnoreCase('key')) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/groovy/se/solrike/cloudformation/StackUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package se.solrike.cloudformation | ||
|
||
|
||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient | ||
import software.amazon.awssdk.services.cloudformation.model.DescribeStackResourceResponse | ||
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksResponse | ||
import software.amazon.awssdk.services.cloudformation.model.Output | ||
|
||
/** | ||
* Utilities for interact with AWS. E.g. Cloudformation stacks | ||
*/ | ||
public class StackUtils { | ||
|
||
/** | ||
* Resolve a AWS Cloudformation stack logical resource ID to a physical resource ID | ||
* | ||
* @param stackName - stackname , e.g. 'my-lambda-stack-for-production' | ||
* @param logicalResourceId - logical resource ID in the stack, e.g. 'ServerlessFunctionArn' | ||
* @return physical resource ID, e.g. 'arn:aws:lambda:eu-north-1:12345:function:slrk-prod-books-api' | ||
*/ | ||
public static String resolveStackResource(String stackName, String logicalResourceId) { | ||
CloudFormationClient client = CloudFormationClient.builder().build() | ||
|
||
DescribeStackResourceResponse response = client.describeStackResource { | ||
it.stackName(stackName) | ||
it.logicalResourceId(logicalResourceId) | ||
} | ||
return response.stackResourceDetail.physicalResourceId | ||
} | ||
|
||
/** | ||
* Resolve an output of a stack using the output's key. | ||
* | ||
* @param stackName - stackname , e.g. 'my-lambda-stack-for-production' | ||
* @param key - e.g. 'LogGroupName' | ||
* @return the value for the key, e.g. '/aws/lambda/slrk-prod-books-api' | ||
* | ||
* @throws CloudFormationException - e.g. if the stack doesn't exists. | ||
* @throws IllegalArgumentException - if the key doesn't exists. | ||
*/ | ||
public static String resolveStackOutput(String stackName, String key) { | ||
CloudFormationClient client = CloudFormationClient.builder().build() | ||
|
||
DescribeStacksResponse response = client.describeStacks { | ||
it.stackName(stackName) | ||
} | ||
Output output = response.stacks().get(0).outputs().find { it.outputKey == key } | ||
if (output) { | ||
return output.outputValue | ||
} | ||
else { | ||
throw new IllegalArgumentException("Cloud not find output with key '$key'.") | ||
} | ||
} | ||
} |