aws-amplify / aws-amplify/amplify-cli

support usage of CDK modules in overrides, receiving `Skipping override due to VMError`

Open
#10,066 5 comments 3 reactions 0 assignees View on GitHub
duplicate extensibility feature-request p3
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-cli/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-cli/blob/master/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

16.13.0

### Amplify CLI Version

7.6.26

### What operating system are you using?

mac

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

### Amplify Categories

storage

### Amplify Commands

override

### Describe the bug

After configuring an S3 bucket with `amplify add storage` and following the prompts, I wanted to apply a policy to my bucket so that anyone can view the uploaded images--not just unauthenticated users.

I ran `amplify override storage` and created a bucket instance (since Amplify generates a CfnBucket) and attached a policy to it.

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

export function override(resources: AmplifyS3ResourceTemplate) {
const productsBucket = s3.Bucket.fromBucketArn(
this,
'productsBucket',
resources.s3Bucket.attrArn
)

productsBucket.addToResourcePolicy(
new iam.PolicyStatement({
actions: ['s3:Get*', 's3:List*'],
resources: [`${resources.s3Bucket.attrArn}/public/productimages/**/*`],
principals: [new iam.AnyPrincipal()],
})
)
}
```

Note that I did not manually import the modules at the top as they are already installed by Amplify.

Upon running `amplify push -y` I received the following error
![image](https://user-images.githubusercontent.com/5106417/160065412-02a9337c-5937-4edd-a7eb-9e7a17abe6dc.png)

### Expected behavior

I would expect the command to push up my resources and the s3 console to reflect the newly added policy.

If I manually add the policy in the console it works but I don't want to introduce drift (I deleted it afterward).

### Reproduction steps

1. `amplify add storage`
2. select read access for any options
3. `amplify override storage`
4. add the following statement

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

export function override(resources: AmplifyS3ResourceTemplate) {
const productsBucket = s3.Bucket.fromBucketArn(
this,
'productsBucket',
resources.s3Bucket.attrArn
)

productsBucket.addToResourcePolicy(
new iam.PolicyStatement({
actions: ['s3:Get*', 's3:List*'],
resources: [`${resources.s3Bucket.attrArn}/public/productimages/**/*`],
principals: [new iam.AnyPrincipal()],
})
)
}
```
5. `amplify push -y`

### GraphQL schema(s)

```graphql
# Put schemas below this line

```

### Log output

```
# Put your logs below this line

Fetching updates to backend environment: dev from the cloud.🛑 Error: Skipping override due to VMError: Cannot find mo
✔ Successfully pulled backend environment dev from the cloud.
⠸ Building resource storage/productsBucket🛑 Error: Skipping override due to VMError: Cannot find module 'os'
```

### Additional information

The option to have public read (for anyone) access should be an option when adding storage.

use case: I am creating a storefront and stripe keeps a list of product URLs so that when a customer checks out it can display the product on their hosted checkout form.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with `amplify add storage`, `amplify override storage`, the supplied TypeScript override, and `amplify push -y`; start from the override command and the `Skipping override due to VMError: Cannot find module 'os'` output. Trace how the override loads CDK modules, then verify that the S3 policy is included in the pushed resources without the VMError.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cli, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.