【问题标题】:Local changes on s3-cloudformation-template.json are overriden on amplify pushs3-cloudformation-template.json 上的本地更改在放大推送时被覆盖
【发布时间】:2022-03-23 16:34:07
【问题描述】:

作为后端配置的一部分,我需要一个 S3 存储桶来使其对象在 1 天后自动过期。我使用 amplify storage add 将 S3 存储桶添加到我的后端,但 AMPLIFY-CLI 有点限制,可以为存储桶配置。

所以,通过放大创建桶后,我打开s3-cloudformation-template.json并手动包含了一个对象过期规则:

{
    "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
                        }
                    ]
                }
            }
        },
    ...
}

之后我发了amplify status,其中检测到cloudformation模板的变化:

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

最后,我发出了amplify push,但命令完成后没有任何关于此更改的 cloudformation 日志,以及用于 S3 存储的 No Change 的新指示:

✔ Successfully pulled backend environment dev from the cloud.

Current Environment: dev

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

再次检查s3-cloudformation-template.json 我注意到我之前添加的配置在push 命令期间被覆盖/删除:

{
    "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": {...}
            }
        },
    ...
}

所以,很确定我在这里犯了一些错误,因为我找不到其他有此问题的帖子,但是,错误在哪里?

【问题讨论】:

    标签: amazon-s3 amazon-cloudformation aws-amplify aws-amplify-cli


    【解决方案1】:

    您需要“覆盖” s3 存储桶配置 (Override Amplify-generated S3)...

    从命令行输入:

    amplify override storage
    

    这将显示:

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

    按 Return 选择是,然后使用以下内容更新 override.ts 文件:

    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
    }
    

    回到命令行,按回车继续:

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

    现在使用以下命令更新后端:

    amplify push
    

    你已经完成了。

    有关配置选项的更多信息,请访问interface LifecycleConfigurationProperty 有关 S3 存储桶还可以覆盖哪些其他内容的更多信息,请访问 class CfnBucket (construct)

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2014-11-23
      • 2013-03-24
      • 2021-09-11
      • 2014-07-07
      • 1970-01-01
      • 2016-12-11
      • 2017-06-27
      • 2015-08-01
      相关资源
      最近更新 更多