This example includes
- Amazon Chime SDK App Instance
- Amazon Chime SDK Channel Flow
- Amazon Chime SDK App Instance User
- Amazon Chime SDK App Instance Admin
- Amazon Chime SDK Streaming Message Data
- Amazon Chime SDK Message Retention
const appInstance = new chime.MessagingAppInstance(this, 'appInstance', {
name: 'MessagingAppInstanceExample',
});
This will create an Amazon Chime SDK Messaging App Instance and will return the AppInstanceArn
.
const appInstanceUser = new chime.MessagingAppInstanceUser(
this,
'appInstanceUser',
{
appInstanceArn: appInstance.appInstanceArn,
appInstanceUserId: '1234',
},
);
This will create an App Instance User that is associated with the previously created Amazon Chime SDK Messaging App Instance. This User can then be promoted to an App Instance Admin.
new chime.MessagingAppInstanceAdmin(this, 'appInstanceAdmin', {
appInstanceAdminArn: appInstanceUser.appInstanceUserArn,
appInstanceArn: appInstance.appInstanceArn,
});
Using the previously created App Instance and the previously created App Instance User, an App Instance Admin can be created.
Take note of the required permissions for the AWS Lambda in the example to allow the Amazon Chime SDK service to invoke the associated Lambda.
const channelFlowLambdaRole = new iam.Role(this, 'channelFlowLambdaRole', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
inlinePolicies: {
['chimePolicy']: new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
resources: [
`arn:aws:chime:${this.region}:${this.account}:app-instance/*`,
],
actions: ['chime:ChannelFlowCallback'],
}),
],
}),
},
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName(
'service-role/AWSLambdaBasicExecutionRole',
),
],
});
const channelFlowHandler = new Function(this, 'channelFlowHandler', {
runtime: Runtime.PYTHON_3_9,
handler: 'channelFlowHandler.lambda_handler',
code: Code.fromAsset('src'),
role: channelFlowLambdaRole,
});
channelFlowHandler.addPermission('Chime Resource Policy', {
principal: new iam.ServicePrincipal('chime.amazonaws.com'),
sourceArn: `arn:aws:chime:${this.region}:${this.account}:app-instance/*`,
sourceAccount: `${this.account}`,
});
const channelFlow = new chime.ChannelFlow(this, 'channelFlow', {
appInstanceArn: appInstance.appInstanceArn,
processors: [
{
name: 'channelFlowName',
configuration: {
lambda: {
resourceArn: channelFlowHandler.functionArn,
invocationType: chime.InvocationType.ASYNC,
},
},
executionOrder: 1,
fallbackAction: chime.FallbackAction.ABORT,
},
],
clientRequestToken: uuidv4(),
});
const kinesisStream = new kinesis.Stream(this, 'kinesisStream', {
streamName: 'chime-messaging-channel-stream',
shardCount: 2,
encryption: kinesis.StreamEncryption.MANAGED,
});
appInstance.streaming([
{
appInstanceDataType: chime.AppInstanceDataType.CHANNEL,
resourceArn: kinesisStream.streamArn,
},
]);
Take note of the requirements for the Kinesis Stream for streaming messaging data:
- Kinesis streams must be in the same AWS account as the AppInstance.
- A stream must be in the same region as the AppInstance.
- Stream names have a prefix that starts with chime-messaging-.
- You must configure at least two shards. Each shard can receive data up to 1MB per second, so scale your stream accordingly.
- You must enable server-side encryption (SSE).
appInstance.retention(2);
Pass a number in days for message retention.