Skip to content

Commit a5fd8f7

Browse files
dgandhi62iliapolo
andauthored
feat(gen2-migration): add imported-resources app (#14709)
* fix(cli-internal): skip imported auth resources in refactor step Imported (reference) auth resources have no nested CloudFormation stack — they reference externally-managed Cognito resources. The refactor step was unconditionally creating an AuthCognitoForwardRefactorer for every auth:Cognito resource, which then failed with "unable to find source stack" because there is no stack to find. Now checks serviceType === 'imported' in amplify-meta.json before creating the refactorer, matching the existing pattern in the generate step. Added tests for both forward and rollback paths. --- Prompt: In the gen2-migration tool, I am getting this error when trying to refactor an app with an imported auth resource - "[AuthCognitoForwardRefactorer] unable to find source stack" * feat: commit snapshots * feat: add pre-refactor snapshot * feat: post refactor snapshot * chore: sanitize fully * chore: add tests * feat: add imported-resources snapshot tests Add generate/refactor snapshots and update CognitoIdentityMock to support imported auth resources. * chore: more mocks * chore: updated snapshiot * chore: update snapshots * chore: sanitize * chore: replace aws cli calls with sdk calls --------- Co-authored-by: Eli Polonsky <epolon@amazon.com>
1 parent 38bca59 commit a5fd8f7

402 files changed

Lines changed: 50847 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
amplify-codegen-temp/models/models
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
26+
#amplify-do-not-edit-begin
27+
amplify/\#current-cloud-backend
28+
amplify/.config/local-*
29+
amplify/logs
30+
amplify/mock-data
31+
amplify/mock-api-resources
32+
amplify/backend/amplify-meta.json
33+
amplify/backend/.temp
34+
build/
35+
dist/
36+
node_modules/
37+
aws-exports.js
38+
awsconfiguration.json
39+
amplifyconfiguration.json
40+
amplifyconfiguration.dart
41+
amplify-build-config.json
42+
amplify-gradle-config.json
43+
amplifytools.xcconfig
44+
.secret-*
45+
**.sample
46+
#amplify-do-not-edit-end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
projects:
2+
amplifytestapp2:
3+
schemaPath: src/graphql/schema.json
4+
includes:
5+
- src/graphql/**/*.ts
6+
excludes:
7+
- ./amplify/**
8+
extensions:
9+
amplify:
10+
codeGenTarget: typescript
11+
generatedFileName: src/API.ts
12+
docsFilePath: src/graphql
13+
extensions:
14+
amplify:
15+
version: 3
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
# Imported Resources (Amplify Gen1)
2+
3+
<img width="625" height="300" src="./images/app.png" />
4+
5+
This is a project board app that supports authentication. Each Project board can hold multiple Todo items,
6+
each of which has a title, description, and optionally, images. Todos do not need to be in a Project
7+
and can exist unassigned.
8+
9+
- Unauthenticated users can only view Projects and Todos, and cannot modify or delete them.
10+
- Authenticated users can create Projects and Todos, and modify/delete their own. They may add
11+
12+
Todos to Projects that are not their own, but cannot change the Project settings.
13+
14+
> [!NOTICE]
15+
> Since amplify operations add files to your local directory, its better not to operate within this repo.
16+
> Instead, create your own private GitHub repository and copy the app over.
17+
18+
## Install Dependencies
19+
20+
```console
21+
npm install
22+
```
23+
24+
## Initialize Environment
25+
26+
```console
27+
amplify init
28+
```
29+
30+
```console
31+
⚠️ For new projects, we recommend starting with AWS Amplify Gen 2, our new code-first developer experience. Get started at https://docs.amplify.aws/react/start/quickstart/
32+
✔ Do you want to continue with Amplify Gen 1? (y/N) · yes
33+
✔ Why would you like to use Amplify Gen 1? · Prefer not to answer
34+
Note: It is recommended to run this command from the root of your app directory
35+
? Enter a name for the project importedresources
36+
The following configuration will be applied:
37+
38+
Project information
39+
| Name: importedresources
40+
| Environment: dev
41+
| Default editor: Visual Studio Code
42+
| App type: javascript
43+
| Javascript framework: none
44+
| Source Directory Path: src
45+
| Distribution Directory Path: dist
46+
| Build Command: npm run-script build
47+
| Start Command: npm run-script start
48+
49+
? Initialize the project with the above configuration? No
50+
? Enter a name for the environment main
51+
? Choose your default editor: Visual Studio Code
52+
✔ Choose the type of app that you're building · javascript
53+
Please tell us about your project
54+
? What javascript framework are you using react
55+
? Source Directory Path: src
56+
? Distribution Directory Path: dist
57+
? Build Command: npm run-script build
58+
? Start Command: npm run-script start
59+
Using default provider awscloudformation
60+
? Select the authentication method you want to use: AWS profile
61+
62+
For more information on AWS Profiles, see:
63+
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
64+
65+
? Please choose the profile you want to use default
66+
```
67+
68+
## Add Categories
69+
70+
### Auth
71+
72+
Create a userPool and identityPool managed outside of Amplify
73+
74+
```console
75+
npm run create-auth
76+
```
77+
78+
```console
79+
amplify import auth
80+
```
81+
82+
Use the ids printed in the previous step
83+
84+
```console
85+
? What type of auth resource do you want to import? Cognito User Pool and Identity Pool
86+
? Select the User Pool you want to import:
87+
✔ Select a Web client to import:
88+
✔ Select a Native client to import:
89+
✔ Only one Identity Pool resource found:
90+
```
91+
92+
93+
### Api
94+
95+
GraphQL API with schema containing:
96+
97+
- _Todo_ model.
98+
- _Project_ model with a `@hasMany` relationship to the _Todo_ model.
99+
- _getRandomQuote_ query that returns inspirational quotes by invoking a
100+
lambda function using the `@function` directive.
101+
102+
```console
103+
amplify add api
104+
```
105+
106+
```console
107+
? Select from one of the below mentioned services: GraphQL
108+
? Here is the GraphQL API that we will create. Select a setting to edit or continue Continue
109+
? Choose a schema template: Single object with fields (e.g., “Todo” with ID, name, description)
110+
✔ Do you want to edit the schema now? (Y/n) · no
111+
```
112+
113+
### Storage
114+
115+
S3-based storage for images of the _Todo_ model. Authenticated users can perform all operations,
116+
unauthenticated users can only read.
117+
118+
```console
119+
amplify add storage
120+
```
121+
122+
```console
123+
? Select from one of the below mentioned services: Content (Images, audio, video, etc.)
124+
✔ Provide a friendly name for your resource that will be used to label this category in the project: · (accept default value)
125+
✔ Provide bucket name: · (accept default value)
126+
✔ Who should have access: · Auth and guest users
127+
✔ What kind of access do you want for Authenticated users? · create/update, read, delete
128+
✔ What kind of access do you want for Guest users? · read
129+
✔ Do you want to add a Lambda Trigger for your S3 Bucket? (y/N) · no
130+
```
131+
132+
### Function
133+
134+
Node.js lambda function that generates inspirational quotes.
135+
136+
```console
137+
amplify add function
138+
```
139+
140+
```console
141+
? Select which capability you want to add: Lambda function (serverless function)
142+
? Provide an AWS Lambda function name: quotegenerator
143+
? Choose the runtime that you want to use: NodeJS
144+
? Choose the function template that you want to use: Hello World
145+
146+
✅ Available advanced settings:
147+
- Resource access permissions
148+
- Scheduled recurring invocation
149+
- Lambda layers configuration
150+
- Environment variables configuration
151+
- Secret values configuration
152+
153+
? Do you want to configure advanced settings? No
154+
? Do you want to edit the local lambda function now? No
155+
```
156+
157+
## Configure
158+
159+
```console
160+
npm run configure
161+
```
162+
163+
## Deploy Backend
164+
165+
```console
166+
amplify push
167+
```
168+
169+
```console
170+
┌──────────┬───────────────────────┬───────────┬───────────────────┐
171+
│ Category │ Resource name │ Operation │ Provider plugin │
172+
├──────────┼───────────────────────┼───────────┼───────────────────┤
173+
│ Api │ importedresources │ Create │ awscloudformation │
174+
├──────────┼───────────────────────┼───────────┼───────────────────┤
175+
│ Auth │ importedresourcesea1b8c4c │ Imported │ awscloudformation │
176+
├──────────┼───────────────────────┼───────────┼───────────────────┤
177+
│ Storage │ s3742db757 │ Create │ awscloudformation │
178+
├──────────┼───────────────────────┼───────────┼───────────────────┤
179+
│ Function │ quotegenerator │ Create │ awscloudformation │
180+
└──────────┴───────────────────────┴───────────┴───────────────────┘
181+
182+
✔ Are you sure you want to continue? (Y/n) · yes
183+
? Do you want to generate code for your newly created GraphQL API No
184+
```
185+
186+
## Publish Frontend
187+
188+
To publish the frontend, we leverage the Amplify hosting console. First push everything to the `main` branch:
189+
190+
```console
191+
git add .
192+
git commit -m "feat: gen1"
193+
git push origin main
194+
```
195+
196+
Next, accept all the default values and follow the getting started wizard to connect your repo and branch.
197+
198+
![](./images/hosting-get-started.png)
199+
![](./images/add-main-branch.png)
200+
![](./images/deploying-main-branch.png)
201+
202+
Wait for the deployment to finish successfully.
203+
204+
## Migrating to Gen2
205+
206+
> Based on https://github.com/aws-amplify/amplify-cli/blob/gen2-migration/GEN2_MIGRATION_GUIDE.md
207+
208+
First and install the experimental CLI package the provides the new commands:
209+
210+
```console
211+
npm install --no-save @aws-amplify/cli-internal-gen2-migration-experimental-alpha
212+
```
213+
214+
Now run them:
215+
216+
```console
217+
npx amplify gen2-migration lock
218+
```
219+
220+
```console
221+
git checkout -b gen2-main
222+
npx amplify gen2-migration generate
223+
```
224+
225+
**Edit in `./amplify/data/resource.ts`:**
226+
227+
```diff
228+
- branchName: "main"
229+
+ branchName: "gen2-main"
230+
```
231+
232+
**Edit in `./amplify/function/quotegenerator/index.js`:**
233+
234+
```diff
235+
- exports.handler = async (event) => {
236+
+ export async function handler(event) {
237+
```
238+
239+
**Edit in `./src/main.tsx`:**
240+
241+
```diff
242+
- import amplifyconfig from './amplifyconfiguration.json';
243+
+ import amplifyconfig from '../amplify_outputs.json';
244+
```
245+
246+
```console
247+
git add .
248+
git commit -m "feat: migrate to gen2"
249+
git push origin gen2-main
250+
```
251+
252+
Now connect the `gen2-main` branch to the hosting service:
253+
254+
![](./images/add-gen2-main-branch.png)
255+
![](./images/deploying-gen2-main-branch.png)
256+
257+
Wait for the deployment to finish successfully. Next, locate the root stack of the Gen2 branch:
258+
259+
![](./images/find-gen2-stack.png)
260+
261+
```console
262+
npm install --no-save @aws-amplify/cli-internal-gen2-migration-experimental-alpha
263+
```
264+
265+
```console
266+
git checkout main
267+
npx amplify gen2-migration refactor --to <gen2-stack-name>
268+
```
269+
270+
```console
271+
git checkout gen2-main
272+
```
273+
274+
**Edit in `./amplify/backend.ts`:**
275+
276+
```diff
277+
- // s3Bucket.bucketName = '...';
278+
+ s3Bucket.bucketName = '...';
279+
```
280+
281+
```console
282+
git add .
283+
git commit -m "chore: post refactor"
284+
git push origin gen2-main
285+
```
286+
287+
Wait for the deployment to finish successfully.
288+
# importedresources
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
node_modules
10+
dist
11+
dist-ssr
12+
*.local
13+
# Editor directories and files
14+
.vscode/*
15+
!.vscode/extensions.json
16+
.idea
17+
.DS_Store
18+
*.suo
19+
*.ntvs*
20+
*.njsproj
21+
*.sln
22+
*.sw?
23+
# amplify
24+
.amplify
25+
amplify_outputs*
26+
amplifyconfiguration*
27+
aws-exports*
28+
build

0 commit comments

Comments
 (0)