aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.47k stars 408 forks source link

Add code snippets for AWS SDKs and AWS Lambda functions #1174

Closed ckant closed 1 year ago

ckant commented 4 years ago

Is your feature request related to a problem? Please describe.

Code snippets can help to accelerate development on AWS using the SDKs.

Developers often use snippets to reduce time spent copying and pasting over boilerplate code. Oftentimes, this boilerplate code must be found and copied from official docs, StackOverflow, etc.

Common actions, such as retrieving an object from S3, often get repeated across multiple code bases.

Describe the solution you'd like

VSCode lets users define custom code snippets to help simplify and speed up the process.

Extensions can also provide code snippets to save the user the trouble and get them on the right track.

By providing code snippets for AWS SDKs, the toolkit is able to offer users a better AWS coding experience, especially when getting started with a particular service integration. Developers can seamlessly get working examples straight from AWS which they can iterate on to fit their usecase.

With AWS Lambda function snippets, developers can quickly generate a function skeleton without needing to consult any documentation. They'll be able to get up and running with Lambda development in just a few keystrokes.

Describe alternatives you've considered

Additional context

Example Lambda function snippet This snippet could be inserted when the user types some form of `aws.lambda`. This sets the user up with the function skeleton for a NodeJS Lambda. ```javascript exports.lambdaHandler = async (event, context) => { $0 }; ```
Example Lambda S3 function snippet (from SAM) This snippet could be inserted when the user types some form of `aws.lambda`. This sets the user up with a working NodeJS Lambda that processes S3 events. ```javascript const AWS = require('aws-sdk'); const s3 = new AWS.S3(); /** * A Lambda function that logs the payload received from S3. */ exports.s3JsonLoggerHandler = async (event, context) => { // All log statements are written to CloudWatch by default. For more information, see // https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html const getObjectRequests = event.Records.map(record => { const params = { Bucket: record.s3.bucket.name, Key: record.s3.object.key }; return s3.getObject(params).promise().then(data => { console.info(data.Body.toString()); }).catch(err => { console.error("Error calling S3 getObject:", err); return Promise.reject(err); }) }); return Promise.all(getObjectRequests).then(() => { //console.debug('Complete!'); }); }; ```
Example AWS SDK snippet #### DynamoDB#GetItem This snippet could be inserted when the user types some form of `aws.dynamodb.getItem`. This adds the boilerplate to create and invoke the SDK (with promises) and wraps the call to catch SDK exceptions. It also provides the user with a sample request payload that they can customize to fit their needs. ```javascript // To read an item from a table // This example retrieves an item from the Music table. The table has a partition key and a sort key (Artist and SongTitle), so you must specify both of these attributes. const dynamoDb = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); try { const response = await dynamoDb .getItem( { Key: { Artist: { S: "Acme Band" }, SongTitle: { S: "Happy Day" } }, TableName: "Music" } ) .promise(); console.log(response); // successful response } catch (err) { console.log(err, err.stack); // an error occurred throw err; } ```

Please +1 if this is a feature that would interest you 😃

justinmk3 commented 1 year ago

Closing since GenAI (CodeWhisperer, etc) covers the main use-case here.