Skip to content

Commit 8af83fa

Browse files
committed
update cdk mentions with Stack.of to backend.<resource>.stack
1 parent a22544c commit 8af83fa

File tree

7 files changed

+123
-187
lines changed

7 files changed

+123
-187
lines changed

src/pages/[platform]/build-a-backend/add-aws-services/logging/set-up-logging/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ The `<log-group-name>` and `<region>` configured in the CDK construct will be us
113113
- `<amplify-authenticated-role-name>` and `<amplify-unauthenticated-role-name>` are Amplify roles created as part of Amplify Auth configuration via Amplify CLI.
114114

115115
```ts
116+
import * as path from "node:path"
116117
import * as cdk from "aws-cdk-lib"
117-
import { Construct } from "constructs"
118118
import * as logs from "aws-cdk-lib/aws-logs"
119-
import * as path from "path"
120119
import * as iam from "aws-cdk-lib/aws-iam"
120+
import { Construct } from "constructs"
121121

122122
export class RemoteLoggingConstraintsConstruct extends Construct {
123123
constructor(scope: Construct, id: string, props: RemoteLoggingConstraintProps) {

src/pages/[platform]/build-a-backend/add-aws-services/predictions/set-up-predictions/index.mdx

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ To learn more, check the docs of [Amazon Translate](https://docs.aws.amazon.com/
3838

3939

4040
```ts title="amplify/backend.ts"
41-
41+
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
4242
import { defineBackend } from "@aws-amplify/backend";
4343
import { auth } from "./auth/resource";
44-
import { Stack } from "aws-cdk-lib";
45-
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
4644

4745
const backend = defineBackend({
4846
auth,
@@ -88,24 +86,21 @@ backend.addOutput({
8886
targetLanguage: "es",
8987
},
9088
proxy: false,
91-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
92-
.region,
89+
region: backend.auth.stack.region,
9390
},
9491
speechGenerator: {
9592
defaults: {
9693
voiceId: "Ivy",
9794
},
9895
proxy: false,
99-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
100-
.region,
96+
region: backend.auth.stack.region,
10197
},
10298
transcription: {
10399
defaults: {
104100
language: "en-US",
105101
},
106102
proxy: false,
107-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
108-
.region,
103+
region: backend.auth.stack.region,
109104
},
110105
},
111106
identify: {
@@ -116,24 +111,21 @@ backend.addOutput({
116111
},
117112
celebrityDetectionEnabled: true,
118113
proxy: false,
119-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
120-
.region,
114+
region: backend.auth.stack.region,
121115
},
122116
identifyLabels: {
123117
defaults: {
124118
type: "ALL",
125119
},
126120
proxy: false,
127-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
128-
.region,
121+
region: backend.auth.stack.region,
129122
},
130123
identifyText: {
131124
defaults: {
132125
format: "ALL",
133126
},
134127
proxy: false,
135-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
136-
.region,
128+
region: backend.auth.stack.region,
137129
},
138130
},
139131
interpret: {
@@ -142,17 +134,14 @@ backend.addOutput({
142134
type: "ALL",
143135
},
144136
proxy: false,
145-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
146-
.region,
137+
region: backend.auth.stack.region,
147138
},
148139
},
149140
},
150141
},
151142
});
152-
153-
154-
155143
```
144+
156145
## Install Amplify Libraries
157146

158147
To install the Amplify library to use predictions features, run the following commands in your project's root folder:

src/pages/[platform]/build-a-backend/auth/moving-to-production/index.mdx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,27 @@ To get started with Amazon SES in production, you must first [request production
4545
After you have configured your account for production access and have verified your _sender_ email, you can configure your Cognito user pool to send emails using the verified sender:
4646

4747
```ts title="amplify/backend.ts"
48-
import { Stack } from "aws-cdk-lib/core"
49-
import { EmailIdentity } from "aws-cdk-lib/aws-ses"
50-
import { defineBackend } from "@aws-amplify/backend"
51-
import { auth } from "./auth/resource"
48+
import { EmailIdentity } from 'aws-cdk-lib/aws-ses';
49+
import { defineBackend } from '@aws-amplify/backend';
50+
import { auth } from './auth/resource';
5251

5352
const backend = defineBackend({
54-
auth,
55-
})
53+
auth
54+
});
5655

57-
const { cfnUserPool } = backend.auth.resources.cfnResources
58-
const authStack = Stack.of(cfnUserPool)
56+
const { cfnUserPool } = backend.auth.resources.cfnResources;
5957

6058
const email = EmailIdentity.fromEmailIdentityName(
61-
authStack,
62-
"EmailIdentity",
59+
backend.auth.stack,
60+
'EmailIdentity',
6361
// your email configured for use in SES
6462
process.env.EMAIL
65-
)
63+
);
6664

6765
cfnUserPool.emailConfiguration = {
68-
emailSendingAccount: "DEVELOPER",
69-
sourceArn: email.emailIdentityArn,
70-
}
66+
emailSendingAccount: 'DEVELOPER',
67+
sourceArn: email.emailIdentityArn
68+
};
7169
```
7270

7371
Now when emails are sent on new user sign-ups, password resets, etc., the sending account will be your verified email.

src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-amazon-rekognition/index.mdx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ export const storage = defineStorage({
7575
To use the Amazon Rekognition service, you need to add Amazon Rekognition as an HTTP Data Source and configure the proper IAM policy for Lambda to effectively utilize the desired feature and grant permission to access the storage. In this case, you can add the `rekognition:DetectText` and `rekognition:DetectLabels` actions to the policy. Update the `amplify/backend.ts` file as shown below.
7676

7777
```ts title= "amplify/backend.ts"
78-
78+
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
7979
import { defineBackend } from '@aws-amplify/backend';
8080
import { auth } from './auth/resource';
8181
import { data } from './data/resource';
82-
import { Stack } from 'aws-cdk-lib';
83-
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
8482
import { storage } from './storage/resource';
8583

8684
const backend = defineBackend({
@@ -89,19 +87,17 @@ const backend = defineBackend({
8987
storage
9088
});
9189

92-
const dataStack = Stack.of(backend.data)
93-
9490
// Set environment variables for the S3 Bucket name
9591
backend.data.resources.cfnResources.cfnGraphqlApi.environmentVariables = {
9692
S3_BUCKET_NAME: backend.storage.resources.bucket.bucketName,
9793
};
9894

9995
const rekognitionDataSource = backend.data.addHttpDataSource(
10096
"RekognitionDataSource",
101-
`https://rekognition.${dataStack.region}.amazonaws.com`,
97+
`https://rekognition.${backend.data.stack.region}.amazonaws.com`,
10298
{
10399
authorizationConfig: {
104-
signingRegion: dataStack.region,
100+
signingRegion: backend.data.stack.region,
105101
signingServiceName: "rekognition",
106102
},
107103
}

src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-amazon-translate/index.mdx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,21 @@ npm add @aws-sdk/client-translate
6060

6161
```ts title="amplify/backend.ts"
6262
import { defineBackend } from '@aws-amplify/backend';
63-
import { auth } from './auth/resource';
64-
import { data } from "./data/resource";
65-
import { Stack } from 'aws-cdk-lib';
6663
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
64+
import { auth } from './auth/resource';
65+
import { data } from './data/resource';
6766

6867
const backend = defineBackend({
6968
auth,
7069
data
7170
});
7271

73-
const dataStack = Stack.of(backend.data)
74-
7572
const translateDataSource = backend.data.addHttpDataSource(
7673
"TranslateDataSource",
77-
`https://translate.${dataStack.region}.amazonaws.com`,
74+
`https://translate.${backend.data.stack.region}.amazonaws.com`,
7875
{
7976
authorizationConfig: {
80-
signingRegion: dataStack.region,
77+
signingRegion: backend.data.stack.region,
8178
signingServiceName: "translate",
8279
},
8380
}

src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-bedrock/index.mdx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ backend.generateHaikuFunction.resources.lambda.addToRolePolicy(
8585
In your `amplify/backend.ts` file, replace the content with the following code to add an HTTP data source for Amazon Bedrock to your API and grant it permissions to invoke a generative AI model:
8686

8787
```ts title="amplify/backend.ts"
88+
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
8889
import { defineBackend } from "@aws-amplify/backend";
8990
import { auth } from "./auth/resource";
9091
import { data } from "./data/resource";
91-
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
92-
import { Stack } from "aws-cdk-lib";
9392

9493
export const backend = defineBackend({
9594
auth,
@@ -103,7 +102,7 @@ const bedrockDataSource = backend.data.addHttpDataSource(
103102
"https://bedrock-runtime.us-east-1.amazonaws.com",
104103
{
105104
authorizationConfig: {
106-
signingRegion: Stack.of(backend.data).region,
105+
signingRegion: backend.data.stack.region,
107106
signingServiceName: "bedrock",
108107
},
109108
}
@@ -114,7 +113,7 @@ bedrockDataSource.grantPrincipal.addToPrincipalPolicy(
114113
effect: Effect.ALLOW,
115114
actions: ["bedrock:InvokeModel"],
116115
resources: [
117-
`arn:aws:bedrock:${Stack.of(backend.data).region}::foundation-model/${MODEL_ID}`,
116+
`arn:aws:bedrock:${backend.data.stack.region}::foundation-model/${MODEL_ID}`,
118117
],
119118
})
120119
);
@@ -352,32 +351,31 @@ const { data, errors } = await client.queries.generateHaiku({
352351
Here's an example of a simple UI that prompts a generative AI model to create a haiku based on user input:
353352

354353
```tsx title="App.tsx"
355-
import { FormEvent, useState } from "react";
356-
357-
import { generateClient } from "aws-amplify/api";
358-
import { Schema } from "@/amplify/data/resource";
359-
360-
import { Amplify } from "aws-amplify";
361-
import outputs from "@/amplify_outputs.json";
354+
import type { Schema } from '@/amplify/data/resource';
355+
import type { FormEvent } from 'react';
356+
import { useState } from 'react';
357+
import { Amplify } from 'aws-amplify';
358+
import { generateClient } from 'aws-amplify/api';
359+
import outputs from '@/amplify_outputs.json';
362360

363361
Amplify.configure(outputs);
364362

365363
const client = generateClient<Schema>();
366364

367365
export default function App() {
368-
const [prompt, setPrompt] = useState<string>("");
366+
const [prompt, setPrompt] = useState<string>('');
369367
const [answer, setAnswer] = useState<string | null>(null);
370368

371-
const sendPrompt = async (e: FormEvent<HTMLFormElement>) => {
372-
e.preventDefault();
369+
const sendPrompt = async (event: FormEvent<HTMLFormElement>) => {
370+
event.preventDefault();
373371

374372
const { data, errors } = await client.queries.generateHaiku({
375-
prompt,
373+
prompt
376374
});
377375

378376
if (!errors) {
379377
setAnswer(data);
380-
setPrompt("");
378+
setPrompt('');
381379
} else {
382380
console.log(errors);
383381
}
@@ -387,17 +385,15 @@ export default function App() {
387385
<main className="flex min-h-screen flex-col items-center justify-center p-24 dark:text-white">
388386
<div>
389387
<h1 className="text-3xl font-bold text-center mb-4">Haiku Generator</h1>
390-
391388
<form className="mb-4 self-center max-w-[500px]" onSubmit={sendPrompt}>
392389
<input
393390
className="text-black p-2 w-full"
394391
placeholder="Enter a prompt..."
395392
name="prompt"
396393
value={prompt}
397-
onChange={(e) => setPrompt(e.target.value)}
394+
onChange={(event) => setPrompt(event.target.value)}
398395
/>
399396
</form>
400-
401397
<div className="text-center">
402398
<pre>{answer}</pre>
403399
</div>
@@ -407,7 +403,7 @@ export default function App() {
407403
}
408404
```
409405

410-
![GIF of a webpage titled "Haiku Generator" and input field. "Frank Herbert's Dune" is entered and submitted. Shortly after, a haiku is rendered to the page.](/images/haiku-generator.gif)
406+
![A webpage titled "Haiku Generator" and input field. "Frank Herbert's Dune" is entered and submitted. Shortly after, a haiku is rendered to the page.](/images/haiku-generator.gif)
411407

412408
## Conclusion
413409

0 commit comments

Comments
 (0)