<jupyter_start><jupyter_text>Create Workshop Notebooks and Users<jupyter_code>import boto3
import botocore
import json
import time
import os
import base64
import docker
import pandas as pd

import project_path # path to helper methods
from lib import workshop
from botocore.exceptions import ClientError

cfn = boto3.client('cloudformation')
iam = boto3.client('iam')

session = boto3.session.Session()
region = session.region_name
account_id = boto3.client('sts').get_caller_identity().get('Account')<jupyter_output><empty_output><jupyter_text>[Create S3 Bucket](https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html)We will create an S3 bucket that will be used throughout the workshop for storing our data.[s3.create_bucket](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.htmlS3.Client.create_bucket) boto3 documentation<jupyter_code>bucket = workshop.create_bucket_name('workshop-')
session.resource('s3').create_bucket(Bucket=bucket, CreateBucketConfiguration={'LocationConstraint': region})
print(bucket)<jupyter_output><empty_output><jupyter_text>Create the IAM Users and SageMaker NotebooksWe will use CloudFormation to create the workshop users and SageMaker Notebooks to run the workshop.<jupyter_code>!cat ../research-env.yml
research_file = 'research-env.yml'
session.resource('s3').Bucket(bucket).Object(os.path.join('cfn', research_file)).upload_file( '../' + research_file)<jupyter_output><empty_output><jupyter_text>Execute CloudFormation Stack to generate Users and EnvironmentCreates a stack as specified in the template. After the call completes successfully, the stack creation starts. You can check the status of the stack via the DescribeStacks API.[cfn.create_stack](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudformation.htmlCloudFormation.Client.create_stack)<jupyter_code>user_count = 2
cfn_template = 'https://s3-{0}.amazonaws.com/{1}/cfn/{2}'.format(region, bucket, research_file)
print(cfn_template)

for x in range(user_count):
    user_stack_name = 'Workshop-' + str(x + 1)
    response = cfn.create_stack(
        StackName=user_stack_name,
        TemplateURL=cfn_template,
        Capabilities = ["CAPABILITY_NAMED_IAM"],
        Parameters=[
            {
                'ParameterKey': 'NotebookInstanceType',
                'ParameterValue': 'ml.t2.medium'
            },
            {
                'ParameterKey': 'UserName',
                'ParameterValue': 'WorkshopUser' + str(x + 1)
            },
            {
                'ParameterKey': 'IAMUserPassword',
                'ParameterValue': 'WorkshopUser#' + str(x + 1)
            }            
            
        ]
    )

    print(response)<jupyter_output><empty_output><jupyter_text>Clean Up<jupyter_code>for x in range(user_count):
    user_stack_name = 'Workshop-' + str(x + 1)
    response = cfn.delete_stack(StackName=user_stack_name)
!aws s3 rb s3://$bucket --force<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11099966566365764, 'fraction_numerical': 0.010698762955533266, 'mean_word_length': 4.2125435540069684, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '54002', 'license': 'MIT-0', 'n_tokens_mistral': 945, 'n_tokens_neox': 908, 'n_words': 186, 'path': 'notebooks/create_workshop.ipynb', 'repo_name': 'bigdatasciencegroup/aws-research-workshops', 'size': '2991'}