mancho-school-infrastructure v2.0.0
Welcome to Mancho School Infrastructure project!
Install dependencies
Install Node.js
One of the dependencies
@mancho-devs/prorab-infrastructure
is in a private Github repository.To be able to pull this package, you need to set your own Github personal access token to
_authToken
in the.npmrc
file on the first line.Note that once you change this tracked file, it will be marked as modified, which in turn will be included in the upcoming commit. You should NOT commit your access token to the repository. In order to ignore this
.npmrc
file, run the following command.git update-index --skip-worktree .npmrc
Install project dependencies.
npm i
Install CDK globally.
npm i -g cdk
Useful commands
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template
Create an AWS Account
By default for consistency purpose we create all our resources in Oregon (us-west-2
) region.
- Create a new Gmail account for infrastruture purpose which will be used for creating a new AWS account. All technical email communications will be held in this email account.
- Sign in to the root account.
- Visit "My Organizations".
- Create a new account.
- Specify the new account name.
- Specify the newly create email address.
- Specify the administrator IAM role name as
AdministratorAccess
.
Sign in to the new AWS account.
- In order to sign in we need to sign out of the root account.
- Sign in as an IAM user in the same root account.
- Once signed in, choose "Switch Roles" under the account name on the top right menu.
- Enter the new account number.
- Enter the administrator IAM role name
AdministratorAccess
.
Notes: If you need to access the new account with the root email, refer to Accessing a member account as the root user which will explain how to reset the root account password.
Visit the IAM console in the new account.
- Create a new IAM administrator user for the newly created AWS account. This will give us access keys and passwords necessary to deploy AWS CDK infrastructure stacks.
- Add the new access key and secret pair into the
~/.aws/credentials
file on your computer. - Configure the AWS SDK in
~/.aws/config
.- Set the region to
us-west-2
. - Set the format to
json
.
- Set the region to
- From now on login using the newly create IAM user of the new account.
Bootstrap the AWS Account and Region
Some infrastructure changes might require staging some files to S3 which is why we need to bootstrap the CDK Toolkit stack with the necessary staging area.
# Mancho School
cdk bootstrap aws://996818757183/us-east-1 --profile mancho-school --app bin/projects/mancho.school/service.js
cdk bootstrap aws://996818757183/us-west-2 --profile mancho-school --app bin/projects/mancho.school/service.js
Populate CDK Context
Some stack need to lookup the default VPC in the account. The VPC information
is stored in the cdk.context.json
file which we can generate using the following
command.
cdk synth --app bin/projects/mancho.school/service.js --profile mancho-school
The above command will populate the VPC values for the Mancho School project. Change
the --profile
and --app
to a different project.
Service Infrastructure Setup
Create CloudFormation Stacks to create resources necessary to setup the service architecture.
The following stacks must be created in this specified order:
DomainsStack
- Resources to manage domains.
- Creates the root hosted zone.
WebsiteStack
- Resources to create a static Website for production stack.
- Creates an
A
record set neccessary forAuthStack
.
BetaWebsiteStack
- A copy of
WebsiteStack
but for beta stack.
- A copy of
AuthStack
- Resources to support authentication.
- Creates a Cognito user pool neccessary for
ServiceAPIStack
.
StorageStack
- Storage resources for API.
- Creates database tables for API.
APIStack
- Lambda function for API.
ServiceAPIStack
- Glue-resources to connect Lambda function with GraphQL server and storage.
npm run test
npm run build
# Mancho School
cdk deploy --profile mancho-school --app bin/projects/mancho.school/dev.js IAMUsersStack
cdk deploy --profile mancho-school --app bin/projects/mancho.school/dev.js IAMStudentsStack
cdk deploy --profile mancho-school --app bin/projects/mancho.school/dev.js IAMStudentsStackTwo
cdk deploy --profile mancho-school --app bin/projects/mancho.school/service.js
Adding a new Student
- First, add the user details at the end of the
./lib/projects/mancho-school/students.ts
or./lib/projects/mancho-school/students-two.ts
files. The reason we have two or more files is that in CloudFormation a single stack cannot store more than 500 resources. The firststudents.ts
file already has enough students that generate around 500 resources. One day we must create the third, fourth and so on files. - Then, deploy
IAMStudentsStack
orIAMStudentsStackTwo
which will create a new IAM user and send an email to the student with AWS authentication credentials. - Then, deploy
${username}StudentWebsiteStack
which will deploy production website for the student. For example,esengineerStudentWebsiteStack
. It is required to deploy the production website first because it will create the website subdomain which the beta subdomain will depend on. - Finally, deploy
${username}WebsitePipelineStack
which will deploy beta website for the student as well as the website pipeline for both stages. For example,esengineerWebsitePipelineStack
.
Removing a Student
- Remove its pipeline stack
${username}WebsitePipelineStack
. - Empty, then remove the S3 bucket created for the student's pipeline stack.
- Remove its website stacks
${username}StudentWebsiteStack
- Remove the user from the
./lib/projects/mancho-school/students.ts
file. - Deploy the
IAMStudentsStack
.
DomainsStack
- Once you deploy the
DomainsStack
, visit the Route53 console. - Choose the newly created hosted zone for your root domain.
- Note the values of the
NS
DNS record. Usually there are four nameservers provided. - Visit the Website of your domain's provider.
- Navigate to your domain page.
- Update the Namespaces fields with the
NS
DNS record values from the Route53 console.
Wait some time (about 10 minutes) for the Amazon Certificate Manager to validate the domain and create an SSL certificate.
AuthStack
When deploying an AuthStack one must first deploy the production stack followed by the beta stack because the domain name in the beta stack requires an A record of the parent root domain to exist.
When implementing the AuthStack
for a project, one needs to decide which sign-in providers are necessary for
authentication purpose. Currently the following providers have been implemented:
AuthStack
in some projects define Lambda based triggers. This means that those AuthStack
depend on CFNPipelineStack
to be deployed in advance. On the other hand CFNPipelineStack
depends on AuthStack
. For this reason it is necessary to first disable triggers in AuthStack
and deploy it. Then deploy CFNPipelineStack
, then enable the triggers in the AuthStack
.
Finally, redeploy the AuthStack
.
- Go to the Google Cloud Platform Credentials page.
- Click Create credentials > OAuth client ID.
- Configure Consent Screen
- Fill the OAuth consent screen.
- For OAuth consent screen click Add or remove scopes
- Choose both
userinfo.email
anduserinfo.profile
options. Press Update. - Save and continue.
- Add Test users.
- Save and continue.
- Back to Credentials
- Create credentials > OAuth client ID.
- Select the Web application application type.
- Name your OAuth 2.0 client.
- For Authorized JavaScript origins enter the custom authorization domain name like
https://auth.<ROOT_DOMAIN>
where<ROOT_DOMAIN>
should be replaced by the new domain name. - Finally, click Create.
- Copy the Client ID and Client Secret.
- Paste the values into the props for the providers.
Manual Operations
Amazon Cognito SMS text message deliveries
SMS sandbox
When we start using Amazon SNS to send SMS messages, out AWS account is in the SMS sandbox. Refer to https://docs.aws.amazon.com/sns/latest/dg/sns-sms-sandbox.html for more details.
We can send SMS only to the phone numbers we have manually entered in the Console. Therefore enter them in the Amazon SNS Console https://us-west-2.console.aws.amazon.com/sns/v3/home?region=us-west-2#/mobile/text-messaging.
Moving out of the SMS sandbox
Moving your AWS account out of the SMS sandbox requires that you first add, verify, and test destination phone numbers. Then, you must create a case with AWS Support.
Refere to https://docs.aws.amazon.com/sns/latest/dg/sns-sms-sandbox-moving-to-production.html for isntructions.
SMS Limits
By default AWS sets a hard spend limit of $1 USD which controls the number of text messages (equivalent to 6 SMS messsages) send from one account. We need to request a limit increase in order to lift this number higher.
- Open a new support case at https://console.aws.amazon.com/support/home#/case/create
- Select "Service limit increase" support case.
- For "Limit type" choose "SNS Text Messaging".
- For "... type of messages..." choose "Transactional".
- For "... AWS Region..." choose "US West (Oregon)".
- Fill out other field if necessary.
- Once the limit is increased, edit the "Text messaging preferences" in the Amazon SNS Console https://us-west-2.console.aws.amazon.com/sns/v3/home?region=us-west-2#/mobile/text-messaging/edit-preferences.
Usually the limit increase happens rather quickly like several minutes.
TODO
- Create Apple application ID for auth.
- Create Google application ID for auth.
1 year ago