S3 Override - accessing CF parameters or project resource ARNs
Note: If your question is regarding the AWS Amplify Console service, please log it in the AWS Amplify Console repository
Which Category is your question related to?
Storage override
Amplify CLI Version
7.6.3
What AWS Services are you utilizing?
S3
Provide additional details e.g. code snippets
Hello,
Is it possible to access parameters passed to the CF template when writing custom overrides? I need a solution that works in Amplify Console for different branches, co I can't use any hardcoded values.
My use case is following:
- I need to modify the
NotificationConfigurationof the S3 bucket. - In the default template, Lambda triggers are defined only for the
public/*,protected/*, andprivate/*prefixes - My backend uses also different prefixes in this S3 bucket and I need to trigger Lambda only when a file is created with a different prefix (at the time it was not possible to add multiple S3 buckets to project. I don't know if this still applies)
- The code in the old CF template looked like this:
"NotificationConfiguration": {
"LambdaConfigurations": [
{
"Event": "s3:ObjectCreated:*",
"Filter": {
"S3Key": {
"Rules": [
{
"Name": "prefix",
"Value": "recommender/"
}
]
}
},
"Function": {
"Ref": "functionrecommendereventrouterArn"
}
}
]
}
- I can't pass the JSON/JS object to the property, since override uses TypeScript and expects
s3.CfnBucket.NotificationConfigurationProperty.
const farn = "How to get the ARN here???";
const notificationConfigurationProperty: s3.CfnBucket.NotificationConfigurationProperty = {
lambdaConfigurations: [
{
event: "s3:ObjectCreated:*",
filter: {
s3Key: {
rules: [
{
name: "prefix",
value: "recommender/"
}
]
}
},
function: farn
}
};
resources.s3Bucket.notificationConfiguration = notificationConfigurationProperty;
- This is not a problem except for the
functionreference that is of a type ofstringand expects the ARN of the trigger function which I can't get from anywhere. - I can't get Amplify to output the construct using the
Reffunction as before, as that would require assigning an object to thefunctionproperty that is of a type ofstring. - If I was able to get to the value of the
functionrecommendereventrouterArnparameter of the CF template, I would be able to do this, but I can't figure out how to access it. - At the moment, I have to construct the ARN of the Labda function I need to call, which is quite cumbersome and I am sure against all best practices. I have an override.template.ts file with placeholders:
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
import * as s3 from '@aws-cdk/aws-s3';
export function override(resources: AmplifyS3ResourceTemplate) {
const env = 'DR_ENV';
const region = 'DR_REGION';
const accountId = 'DR_ACCOUNT';
const farn = `arn:aws:lambda:${region}:${accountId}:function:recommendereventrouter-${env}`;
const notificationConfigurationProperty: s3.CfnBucket.NotificationConfigurationProperty = {
lambdaConfigurations: [
{
event: "s3:ObjectCreated:*",
filter: {
s3Key: {
rules: [
{
name: "prefix",
value: "recommender/"
}
]
}
},
function: farn
}
};
resources.s3Bucket.notificationConfiguration = notificationConfigurationProperty;
}
And I have an Amplify prebuild script that uses sed to fill in the placeholders with the actual values from the environment variables:
#! /bin/bash
echo "Storage prebuild..."
cd ./amplify/backend/storage/dreamroiproductmedia/
cp ./override.template.ts ./override.ts
echo $AWS_BRANCH_ARN
ARN_ARR=($(echo $AWS_BRANCH_ARN | tr ":" "\n"))
#echo $ARN_ARR
#echo ${ARN_ARR[4]}
ACCOUNT=${ARN_ARR[4]}
REGION=${ARN_ARR[3]}
echo $REGION
echo $ACCOUNT
echo $AWS_BRANCH
sed -i 's/DR_ACCOUNT/'"$ACCOUNT"'/g' override.ts
sed -i 's/DR_REGION/'"$REGION"'/g' override.ts
sed -i 's/DR_ENV/'"$AWS_BRANCH"'/g' override.ts
echo "Resulting override.ts:"
cat override.ts
I don't like this solution. Is there a better way?
Thanks for your help!
Hi @oookoook
Based on old CFN template :
"NotificationConfiguration": {
"LambdaConfigurations": [
{
"Event": "s3:ObjectCreated:*",
"Filter": {
"S3Key": {
"Rules": [
{
"Name": "prefix",
"Value": "recommender/"
}
]
}
},
"Function": {
"Ref": "functionrecommendereventrouterArn"
}
}
]
}
If this parameter is present in the CFN , you can directly Ref this in the override.ts file. It should look like this:
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
import * as s3 from '@aws-cdk/aws-s3';
export function override(resources: AmplifyS3ResourceTemplate) {
const currentsettings = resources.s3Bucket.notificationConfiguration as s3.CfnBucket.NotificationConfigurationProperty;
const previousLambdaConfig = currentsettings.lambdaConfigurations as s3.CfnBucket.LambdaConfigurationProperty[];
const farn = currentsettings.lambdaConfigurations[0].function;
const newConfig = {
event: "s3:ObjectCreated:*",
filter:{
s3Key:{
rules:[
{
name: "prefix",
value: "recommender/"
}
]
}
},
function: farn
} as s3.CfnBucket.LambdaConfigurationProperty;
// here we appends custom config with amplify generated config
previousLambdaConfig.push(newConfig);
const notificationConfigurationProperty: s3.CfnBucket.NotificationConfigurationProperty = {
lambdaConfigurations: previousLambdaConfig
};
resources.s3Bucket.notificationConfiguration = notificationConfigurationProperty;
}
Keep in mind this piece keeps the old settings of triggers intact and add the additional lambda config. If you want to remove the previous config , you can try this:
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
import * as s3 from '@aws-cdk/aws-s3';
export function override(resources: AmplifyS3ResourceTemplate) {
const currentsettings = resources.s3Bucket.notificationConfiguration as s3.CfnBucket.NotificationConfigurationProperty;
const farn = currentsettings.lambdaConfigurations[0].function;
const newConfig = {
event: "s3:ObjectCreated:*",
filter:{
s3Key:{
rules:[
{
name: "prefix",
value: "recommender/"
}
]
}
},
function: farn
} as s3.CfnBucket.LambdaConfigurationProperty;
const notificationConfigurationProperty: s3.CfnBucket.NotificationConfigurationProperty = {
lambdaConfigurations: newConfig
};
resources.s3Bucket.notificationConfiguration = notificationConfigurationProperty;
}
let me know if this works for you?
Hello, @akshbhu thanks a lot! What I was missing was the cast of the current configuration - resources.s3Bucket.notificationConfiguration as s3.CfnBucket.NotificationConfigurationProperty - it never occurred to me that I can do that.. I've tried it right now and it seems to work as expected and it solved my problem.
Thanks for your help!
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.