简体   繁体   English

如何使用 aws-sdk 从 lambda function 中删除 s3 object

[英]How to delete an s3 object from a lambda function using aws-sdk

I am trying to delete an object from an s3 bucket, but no matter what I do the delete request always ends up timing out.我正在尝试从 s3 存储桶中删除 object,但无论我做什么,删除请求总是会超时。 I'm thinking I've either configured the permissions incorrectly or I'm using the aws-sdk incorrectly.我在想我要么错误地配置了权限,要么我错误地使用了 aws-sdk。

Here is my lambda function creation, defined in aws-cdk:这是我的 lambda function 创建,在 aws-cdk 中定义:

this.appsyncS3LambdaResolver = new NodejsFunction(
      this,
      "appsyncS3LambdaResolver",
      {
        memorySize: 1024,
        handler: "handler",
        runtime: lambda.Runtime.NODEJS_14_X,
        timeout: cdk.Duration.seconds(5),
        entry: __dirname + "/../../lambda-fns/AppsyncS3LambdaResolver/index.ts",
        environment: {
          SECRET_NAME: props.rdsSecretName || "",
          SECRET_VALUE: props.rdsSecretValue || "",
          S3_BUCKET_NAME: props.s3bucket.bucketName,
          S3_BUCKET_URL: props.s3bucket.bucketWebsiteUrl,
        },
        bundling: {
          externalModules: ["aws-sdk"],
          nodeModules: ["pg"],
        },
        vpc: props.vpc,
        vpcSubnets: { subnetType: ec2.SubnetType.ISOLATED },
        securityGroups: [props.lambdaAccessToRDSSecurityGroup],
      }
    );

    // Give appsyncS3LambdaResolver access to put to S3 bucket (which enables it to make presigned urls)
    // and delete
    props.s3bucket.grantPut(this.appsyncS3LambdaResolver);
    props.s3bucket.grantDelete(this.appsyncS3LambdaResolver);

And here is my s3 bucket creation:这是我的 s3 存储桶创建:

this.s3bucket = new s3.Bucket(this, "s3-bucket", {
      // bucketName: 'my-bucket',
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      autoDeleteObjects: true,
      versioned: false,
      publicReadAccess: false,
      encryption: s3.BucketEncryption.S3_MANAGED,
      cors: [
        {
          allowedMethods: [s3.HttpMethods.GET, s3.HttpMethods.PUT],
          allowedOrigins: props.isProd
            ? [] // tbd
            : ["http://localhost:3000", "http://localhost:3000/*"],
          allowedHeaders: ["*"],
        },
      ],
      lifecycleRules: [
        {
          abortIncompleteMultipartUploadAfter: cdk.Duration.days(90),
          expiration: cdk.Duration.days(365),
          transitions: [
            {
              storageClass: s3.StorageClass.INFREQUENT_ACCESS,
              transitionAfter: cdk.Duration.days(30),
            },
          ],
        },
      ],
    });

    this.s3bucket.addToResourcePolicy(
      new iam.PolicyStatement({
        sid: "allow deleting objects from s3 bucket /public/*",
        effect: iam.Effect.ALLOW,
        principals: [new iam.AnyPrincipal()],
        actions: ["s3:DeleteObject"],
        resources: [this.s3bucket.bucketArn + "/public/*"],
      })
    );

And the actual lambda function code which isn't deleting:实际的 lambda function 代码没有被删除:

for (let i = 0; i < result.rows[0].num_media; i++) {
        const params = {
          Bucket: process.env.S3_BUCKET_NAME,
          Key: `public/reviewmedia/${reviewId}/${i}`,
        };

        console.log("params:", params);

        const res = await s3.deleteObject(params).promise();
        console.log(res);
      }

I have the two permissions to grant my lambda function access to delete from the s3 bucket (s3bucket.grantDelete() and the policy on the s3 bucket) but neither of them seem to work.我有两个权限来授予我的 lambda function 访问权限以从 s3 存储桶中删除(s3bucket.grantDelete() 和 s3 存储桶上的策略),但它们似乎都不起作用。 Here I have given my policy the equivalent of principals: "*" but that didn't fix it either.在这里,我给了我的策略等同于委托人:“*”,但这也没有解决它。 I'm not sure what's wrong with my configuration... I would really appreciate some advice.我不确定我的配置有什么问题......我非常感谢一些建议。

Usually timeout errors are related to connectivity issues.通常超时错误与连接问题有关。

In case of lambda running in VPC, make sure the associated SG allows outbound traffic and also check the lambda subnets has a route to connect to S3 (via IGW for public subnets, Nat Gateway/Nat Instance for private subnets or S3 VPC Endpoint to connect to S3 privately without requiring options mentioned before).如果 lambda 在 VPC 中运行,请确保关联的 SG 允许出站流量,并检查 lambda 子网是否有连接到 S3 的路由(通过 IGW 用于公共子网,Nat Gateway/Nat 实例用于私有子网或 S3 VPC 端点连接私下连接到 S3,而不需要前面提到的选项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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