Skip to content

Commit b156b2b

Browse files
authored
update cdk mentions with Stack.of to backend.<resource>.stack (#8017)
1 parent d593c37 commit b156b2b

File tree

6 files changed

+111
-175
lines changed
  • src/pages/[platform]/build-a-backend

6 files changed

+111
-175
lines changed

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

+2-2
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

+9-22
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export function getStaticProps() {
2525
};
2626
}
2727

28-
### Set up the backend
29-
3028
To enable Predictions we need to set up the appropriate IAM policy for Roles in your Cognito Identity Pool in order to use an appropriate feature. Additionally, we need to use the ```addOutput``` method to patch the custom Predictions resource to the expected output configuration.
3129

3230
<Callout informational>
@@ -38,11 +36,9 @@ To learn more, check the docs of [Amazon Translate](https://docs.aws.amazon.com/
3836

3937

4038
```ts title="amplify/backend.ts"
41-
39+
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
4240
import { defineBackend } from "@aws-amplify/backend";
4341
import { auth } from "./auth/resource";
44-
import { Stack } from "aws-cdk-lib";
45-
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
4642

4743
const backend = defineBackend({
4844
auth,
@@ -88,24 +84,21 @@ backend.addOutput({
8884
targetLanguage: "es",
8985
},
9086
proxy: false,
91-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
92-
.region,
87+
region: backend.auth.stack.region,
9388
},
9489
speechGenerator: {
9590
defaults: {
9691
voiceId: "Ivy",
9792
},
9893
proxy: false,
99-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
100-
.region,
94+
region: backend.auth.stack.region,
10195
},
10296
transcription: {
10397
defaults: {
10498
language: "en-US",
10599
},
106100
proxy: false,
107-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
108-
.region,
101+
region: backend.auth.stack.region,
109102
},
110103
},
111104
identify: {
@@ -116,24 +109,21 @@ backend.addOutput({
116109
},
117110
celebrityDetectionEnabled: true,
118111
proxy: false,
119-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
120-
.region,
112+
region: backend.auth.stack.region,
121113
},
122114
identifyLabels: {
123115
defaults: {
124116
type: "ALL",
125117
},
126118
proxy: false,
127-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
128-
.region,
119+
region: backend.auth.stack.region,
129120
},
130121
identifyText: {
131122
defaults: {
132123
format: "ALL",
133124
},
134125
proxy: false,
135-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
136-
.region,
126+
region: backend.auth.stack.region,
137127
},
138128
},
139129
interpret: {
@@ -142,17 +132,14 @@ backend.addOutput({
142132
type: "ALL",
143133
},
144134
proxy: false,
145-
region: Stack.of(backend.auth.resources.unauthenticatedUserIamRole)
146-
.region,
135+
region: backend.auth.stack.region,
147136
},
148137
},
149138
},
150139
},
151140
});
152-
153-
154-
155141
```
142+
156143
## Install Amplify Libraries
157144

158145
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/data/custom-business-logic/connect-amazon-rekognition/index.mdx

+3-7
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

+4-7
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

+16-20
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)