I'm using CloudFormation to set up a DocumentDB cluster inside a new VPC. CloudFormation is setting up everything, including the VPC, the DocumentDB password secret, and the secret's rotation. I'm following closely the DocumentDB example at AWS::SecretsManager::RotationSchedule
.
I'm at the point where I'm setting up the AWS::SecretsManager::RotationSchedule HostedRotationLambda
for MongoDBSingleUser
. The confusing bit is the VpcSecurityGroupIds
, which says:
A comma-separated list of security group IDs applied to the target database.
The templates applies the same security groups as on the Lambda rotation function that is created as part of this stack.
The example seems to use Fn::GetAtt
to get the default security group of the VPC.
Fn::GetAtt:
- TestVPC
- DefaultSecurityGroup
But I've set up a special security group just for the database, allowing ingress on port 27017
for DocumentDB. Wouldn't I want to simply reference this MyCustomDocumentDBSecurityGroup
for the lambda, rather than the VPC's default security group?
I guess the crucial part I'm missing is that I don't understand the purpose of VpcSecurityGroupIds
. It looks like it's assigning the security group to the lambda. But I'm not sure why the lambda would need a security group. Or rather, it would seem that the lambda would need the inverse of the database's security group—it needs egress to port 27017
, not ingress, in order to connect to the database and update its password, no?
After reading the documentation again, it sounds like this security group is placed on the lambda when it is created, and then when the lambda runs it applies that same security group to the database. But why? My template already applies that security group to the database. Maybe this is to apply another security group to the database after rotation? But why?
I see that VpcSecurityGroupIds
is optional. If I just leave it off, will everything be fine and the database will keep the security group I've already assigned? But then why does the example assign a security group?
Update: From the comments here it would seem that I should provide a separate security group for the lambda in order for it to access the VPC endpoint. But Set up automatic rotation for Amazon RDS, Amazon Redshift, or Amazon DocumentDB secrets using the console says "The rotation functions … have the same VPC and security group as the database …." That seems to indicate I should just set the lambda to use the security group I created for the database. The documentation is very confusing and inconsistent.