AWS: Get secrets manager output from another account
Use case description
We are using the ssm functionality to access secrets in the local account but would also like to access aws secrets manager keys in another account.
This can be done using aws cli/sdk directly by arn i.e. arn:aws:secretsmanager:REGION:SECRET_ACCOUNT_ID:secret:SECRET_ID but not by ssm syntax.
Hello @buddhabuddy :wave: Thanks for submitting your request. I think it's a valid proposal as we're already supporting referencing ssm variables in another regions https://www.serverless.com/framework/docs/providers/aws/guide/variables/#reference-variables-using-the-ssm-parameter-store
The one thing I'm not sure about is the potential syntax that would support this case. As of right now, we support ssm.REGION:/path/to/param when specifying variable in a different region, one option would be to support ssm.REGION.ACCOUNT:/path/to/param, which will also require always supplying region and account when referencing variables from different account. Alternatively, we could also potentially try to support both ssm.REGION and ssm.ACCOUNT and try to detect which version is provided, but that can be potenially a bit more tricky.
I'd love to get your opinion on this one @medikoo before moving forward with potential implementation proposal
@pgrzesik good points! As I think of it, I believe we can add support for account, but first we should normalize a bit our variables syntax. At least to me how ssm takes region is sort of out of convention, we use in other contexts (e.g. file).
I think we should unify our variables syntax to format as <scope>(<params>):<address>
Having that, ssm could take region via ssm(<region>):<path>, and i believe that having that we could support account via ssm(<account>:<region>):<path>. Here it's the question whether we should support <account>(different from one used by service) without <region>, if it's valid to assume same region as service is in, then I would detect whether we deal with an account or region with rule that numeric value is an account.
I would consider addressing it after we have new variables engine (soon to be addressed at https://github.com/serverless/serverless/issues/8364). So let's put it on hold now, and after it's done we can mark it as help wanted.
I like the idea of unification our variables syntax. The one thing that I was also pondering about is to introduce named params instead of relying on ordering and/or detecting them based on format. So, for example, we could support <scope>(<param>=<value>,<param2>=<value2>):<address>. What do you think?
In any way, I agree that we should wait with such changes until we have new variables engine in place.
we could support
<scope>(<param>=<value>,<param2>=<value2>):<address>. What do you think?
That's worth considering, we loose the brevity, but at least it's clear what values represent. I suggest to revisit once new variables engine is ready
@medikoo With the new variables engine sorted do you think this could be achievable?
@buddhabuddy I believe so. ssm resolver for new engine is configured here: https://github.com/serverless/serverless/blob/6f88c11312c67b628c398105af0ea466642731e7/lib/configuration/variables/sources/instance-dependent/get-ssm.js
One question is, will it require different AWS credentials to resolve SSM params from different accounts?
We attach permissions to the subaccount/role to get key and decrypt for the cross account access via lambda so don't need to switch roles/account
@buddhabuddy currently supported format is ssm([<region>]):path.
I think we can provide support for account, by adding support for following form:
ssm([account=<account>][, region=<region>]):path # ofc order irrelevant
Will that work for you? Will you be willing to create PR with support for that?
Yeah that would be great! I'll get on it
@buddhabuddy we've discussed this issue internally yesterday and we'd like to gauge interest from the community on that feature first (we prioritize part of the roadmap based on community feedback).
A simple way to express interest in that feature is to add a 👍 on the top comment.
Nonetheless, you can achieve it today going one of the following routes:
- Resolve those SSM variables through
filevariable, as then as resolve you may provide a JS function: https://www.serverless.com/framework/docs/providers/aws/guide/variables#exporting-a-function - Define custom variable source dedicated for multi-account SSM resolution
Is this feature in the roadmap ? it would be nice to have
@xriu PR's welcome!
Yeah, I'd been to see this. My organisation has just implement a multi-account architecture keeping secrets and system vars in a highly protected account. I've now got a headache on how to support this with serverless architecture!
Is it weird that we're considering extending the SSM param resolver to handling cross-account Secrets Manager secrets?
Secrets Manager secrets can be configured for cross-account access, but SSM doesn't seem easily configured in the same way.
CloudFormation deals with this problem with the resolve statement, using what they call dynamic references. The basic patterns are:
'{{resolve:ssm:parameter-name:version}}'
'{{resolve:secretsmanager:secret-id:secret-string:json-key:version-stage:version-id}}'
It feels to me that if we follow the pattern of ssm([account=<account>][, region=<region>]):path then we might be misleading folks into thinking that we'll resolve their cross-account handling for SSM params, too, when this is intended to only tackle secrets-manager secrets.
Would you be amenable to an introducing a new secretsmanager keyword that follows the same sort of pattern as the cloudformation dynamic reference? The main difference from AWS's solution is that we'd resolve earlier during packaging, much like the SSM resolution.