Open ohookins opened 2 weeks ago
So far as a workaround to be able to install it, I have found this workaround:
The need for this is contingent on there being undocumented SES behaviour that tests the writability of the destination S3 bucket when the receipt rule is being created. The only way I can see around this without having to do a multiple stage deploy as I've used above, would be to add an IAM role to the SES configuration.
This would not allow us to use an IAM assume role policy with a calculated principle from the other resources - we'd need to form the template string ourselves without making reference to the receipt rule (thus introducing a resource ordering dependency). It's a bit icky but would likely work around this problem.
I got this response from AWS support:
Yes, The SES service is performing a test operation to the bucket by sending an AMAZON_SES_SETUP_NOTIFICATION message. Usually when creating a receiving rule set in SES, the SES service sends the initial setup notification message to your S3 bucket with the below details to confirm if it can write to it.
So I think fundamentally the ReceiptRule CDK resource introduces a race condition that cannot be escaped. I think the only workaround for this would be to create an IAM role for SES to use, which grants S3 permissions required, and is created and configured prior to setting up the ReceiptRule.
I'm not 100% sure of the solution here although I'm reasonable certain about most of the cause.
I'm running into an issue deploying the app stack, which gets to about 90/98 resources in the stack before it fails with an error like:
What I believe is happening is that the receipt rule is triggering an attempt by SES to write to the RUA bucket, in order to test that the S3 permissions are correct. The S3 bucket at this point in time doesn't have a bucket policy to allow it, nor does the receipt rule use an IAM role that grants those permissions. It fails, and the receipt rule therefore doesn't create properly and the whole stack is rolled back.
This assumes that there is some automatic S3 write test going on in SES, but I can't imagine any other reason for this. It's worth mentioning that the call to cdk.aws_ses_actions.S3 should actually add an appropriate bucket policy for the access, as per this code in CDK.
My thoughts around why this doesn't work:
I've tried but failed to create additional policy statements but due to the dependency ordering I don't think they end up being created early enough. Incidentally I noticed some "fixes" in CDK that relate to this functionality:
The second of these is a revert of the first PR. The first PR implements the "correct" SES IAM policy for the bucket with enough specificity, but ironically would create even more of a circular dependency - you need to know the receipt rule ARN in order to create the bucket policy to give it access, but if the creation of the receipt rule tests S3 write access, you have already failed at that point.
I also notice there is a custom resource in place to activate and deactivate the ruleset? Maybe a similar approach can be used to enable/disable the receipt rule? I will attempt to diagnose further.