简体   繁体   中英

Local changes on s3-cloudformation-template.json are overriden on amplify push

As part of my backend configuration, I need that an S3 bucket to get its objects automatically expired after 1 day. I included the S3 bucket to my backend with amplify storage add , but AMPLIFY-CLI is a bit limited on which can be configured for buckets.

So, after creating the bucket through amplify, I opened s3-cloudformation-template.json and manualy included a rule for object expiration:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "S3 resource stack creation using Amplify CLI",
    "Parameters": {...},
    "Conditions": {...},
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            
            "DependsOn": [
                "TriggerPermissions"
            ],
            
            "DeletionPolicy" : "Retain",
            "Properties": {
                "BucketName": {...},
                
                "NotificationConfiguration": {...},
                
                "CorsConfiguration": {...},

                "LifecycleConfiguration": {
                    "Rules": [
                        {
                            "Id": "ExpirationRule",
                            "Status": "Enabled",
                            "ExpirationInDays": 1
                        }
                    ]
                }
            }
        },
    ...
}

After that, I issued an amplify status , where the change in the cloudformation template was detected:

| Category | Resource name       | Operation | Provider plugin   |
| -------- | ------------------- | --------- | ----------------- |
| Storage  | teststorage         | Update    | awscloudformation |

Finally, I issued an amplify push but the command finished without any cloudformation logs for this change, and a new indication of No Change for the S3 storage:

✔ Successfully pulled backend environment dev from the cloud.

Current Environment: dev

| Category | Resource name       | Operation | Provider plugin   |
| -------- | ------------------- | --------- | ----------------- |
| Storage  | teststorage         | No Change | awscloudformation |

Checking s3-cloudformation-template.json again I noticed that the configuration I've added before was overriden/removed during the push command:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "S3 resource stack creation using Amplify CLI",
    "Parameters": {...},
    "Conditions": {...},
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            
            "DependsOn": [
                "TriggerPermissions"
            ],
            
            "DeletionPolicy" : "Retain",
            "Properties": {
                "BucketName": {...},
                
                "NotificationConfiguration": {...},
                
                "CorsConfiguration": {...}
            }
        },
    ...
}

So, pretty sure I'm making some mistake here since I couldn't find other posts with this problem, but, where is the mistake?

You need to "override" the s3 bucket configuration ( Override Amplify-generated S3 )...

From the command line type:

amplify override storage

This will show:

✅ Successfully generated "override.ts" folder at C:\myProject\amplify\backend\storage\staticData
√ Do you want to edit override.ts file now? (Y/n) · yes

Press Return to choose yes then update the override.ts file with the following:

import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper'
import * as s3 from '@aws-cdk/aws-s3'

export function override(resources: AmplifyS3ResourceTemplate) {
  const lifecycleConfigurationProperty: s3.CfnBucket.LifecycleConfigurationProperty =
    {
      rules: [
        {
          status: 'Enabled',
          id: 'ExpirationRule',
          expirationInDays: 1
        }
      ]
    }
  resources.s3Bucket.lifecycleConfiguration = lifecycleConfigurationProperty
}

Back to the command line, press Return to continue:

Edit the file in your editor: C:\myProject\amplify\backend\storage\staticData\override.ts
? Press enter to continue

Now update the backend using:

amplify push

And you're done.

Further information on the configuration options can be found at interface LifecycleConfigurationProperty Further information on what else can be overridden for the S3 bucket can be found at class CfnBucket (construct)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM