amazon-s3 - s3-cloudformation-template.json 上的本地更改在放大推送时被覆盖

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

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

因此,在通过 amplify 创建存储桶后,我打开了 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 存储出现了无更改的新指示:

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

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

最佳答案

您需要“覆盖”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 键选择 yes,然后使用以下内容更新 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)

关于amazon-s3 - s3-cloudformation-template.json 上的本地更改在放大推送时被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68867574/

相关文章:

python - Metaflow 数据对象未存储在 S3 中

amazon-web-services - 无停机 AWS + ECS Cloudformation

amazon-web-services - 如何使用 AWS codedeploy 将多个应用程序部署到一个实例

aws-cloudformation - 自动轮换 AppSync API key

aws-lambda - 放大 CUSTOM_AUTH 以及用户名/密码失败并出现 NotAuthorizedException

ios - 如何在我的 iOS/Swift 项目中调用我的 AWS API Gateway REST API

python - 获取 botocore.exceptions.NoCredentialsError : Unable to locate credentials especially when I using CronTab

javascript - Axios:上传到 s3 在上传完成之前解析

objective-c - 如何使用 Objective-c 在 Amazon S3 上创建文件夹

reactjs - 为 React 自定义 AWS Amplify 身份验证 UI