LoginSignup
9
5

More than 5 years have passed since last update.

Serverless Frameworkで既存のS3バケットにトリガーをアタッチする

Last updated at Posted at 2017-06-30

Serverless Frameworkで既存のS3バケットにトリガーをアタッチしようとすると、デプロイ時に以下のようなエラーが出てしまった。

Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (3.85 KB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........................Serverless: Deployment failed!

  Serverless Error ---------------------------------------

     An error occurred while provisioning your stack: S3BucketServerlessexample
     - mybucket already exists
     in stack arn:aws:cloudformation:us-east-1:872755943855:stack/example-dev/06bf9a10-f42b-11e6-a0f4-500c221b72d1.

CloudFormation側の制限により、既存のS3バケットに手を加えることができないらしい。

解決策

結論としてはserverless-external-s3-eventというプラグインを使った。使い方はREADMEがわかりやすいので割愛する。

デプロイコマンドのあとに、sls s3deployを流す必要があるので、以下のようなコマンドをまとめて実行するyarnコマンドだけ作成した。

sls deploy --stage development && sls s3deploy --stage development

IAM権限を絞っている場合は、以下のように該当バケットに対して権限を加える必要がある。

{
    "Effect": "Allow",
    "Action": [
        "s3:Get*",
        "s3:List*",
        "s3:PutBucketNotification"
    ],
    "Resource": [
        "arn:aws:s3:::BUCKET-NAME",
        "arn:aws:s3:::BUCKET-NAME/*"
    ]
}

これで、既存のS3バケットにもトリガーを設置することができた。

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5