Created
December 23, 2023 19:57
-
-
Save moregeek/b7f23f28596ce677bbfd571e8e958974 to your computer and use it in GitHub Desktop.
Serverless Plugin that stringify's a JSON input
This file contains hidden or 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
'use strict'; | |
class ServerlessPluginJSON { | |
constructor() { | |
this.configurationVariablesSources = { | |
jsonStringify: { | |
async resolve({ params }) { | |
return { | |
value: JSON.stringify( (params[0] || {}) ), | |
}; | |
}, | |
}, | |
jsonParse: { | |
async resolve({ params }) { | |
return { | |
value: JSON.parse( (params[0] || "") ), | |
}; | |
}, | |
}, | |
}; | |
} | |
} | |
module.exports = ServerlessPluginJSON; |
This file contains hidden or 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
plugins: | |
- ./serverless-plugin-json.js | |
service: demo | |
custom: | |
# config key to store in Parameter Store | |
myConfigKey: | |
- name: 'app_instance_1' | |
group_map: | |
- azure_group: 'instance1_viewer' | |
app_role: 'viewer' | |
- azure_group: 'app_admin' | |
app_role: 'admin' | |
- name: 'app_instance_2' | |
group_map: | |
- azure_group: 'instance2_viewer' | |
app_role: 'viewer' | |
- azure_group: 'app_admin' | |
app_role: 'admin' | |
example_1: ${jsonStringify(${self:custom.myConfigKey})} | |
# => "[{\"name\":\"app_instance_1\",\"group_map\":[{\"azure_group\":\"instance1_viewer\",\"app_role\":\"viewer\"},{\"azure_group\":\"app_admin\",\"app_role\":\"admin\"}]},{\"name\":\"app_instance_2\",\"group_map\":[{\"azure_group\":\"instance2_viewer\",\"app_role\":\"viewer\"},{\"azure_group\":\"app_admin\",\"app_role\":\"admin\"}]}]", | |
example_2: ${jsonStringify()} | |
# => {} | |
example_3: ${jsonParse(${self:custom.example_1})} | |
# => [ | |
# { | |
# "name": "app_instance_1", | |
# "group_map": [ | |
# { | |
# "azure_group": "instance1_viewer", | |
# "app_role": "viewer" | |
# }, | |
# { | |
# "azure_group": "app_admin", | |
# "app_role": "admin" | |
# } | |
# ] | |
# }, | |
# { | |
# "name": "app_instance_2", | |
# "group_map": [ | |
# { | |
# "azure_group": "instance2_viewer", | |
# "app_role": "viewer" | |
# }, | |
# { | |
# "azure_group": "app_admin", | |
# "app_role": "admin" | |
# } | |
# ] | |
# } | |
# ] | |
provider: | |
name: aws | |
stage: ${opt:stage, 'test'} | |
resources: | |
Resources: | |
SSMParameter: | |
Type: AWS::SSM::Parameter | |
Properties: | |
Name: '/${self:provider.stage}/${self:service}/some_json_parameter_that_is_stored_as_string' | |
Type: String | |
Value: ${jsonStringify( ${self:custom.myConfigKey} )} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment